-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate Flow and Typescript types #2374
Comments
This is for user graphql queries. |
I'd add just plain ol "schema generation" as well here, for integration into tooling like https://github.com/apollographql/eslint-plugin-graphql |
I’ve got a task for this in the typescript starter ;) I think it’s working for flow too ;) (I need to check on my computer) |
So yeah it's works for flow -> https://github.com/dotansimha/graphql-code-generator BTW, what is the goal of this issue? Improve docs, generate flow/typescript types with gatsby-cli or anything else? 😄 |
The Relay Compilier has support now for generating Typescript types https://github.com/relay-tools/relay-compiler-language-typescript |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
Originally I tried to use graphql`
query AlbumQuery($id: String!) {
album(id: { eq: $id }) {
id
name
} Will produce an error like: ERROR: RelayCodeGenerator: Complex argument values (Lists or InputObjects with nested variables) are not supported, argument `id` had value:
{
"kind": "ObjectValue",
"metadata": null,
"fields": [
{
"kind": "ObjectFieldValue",
"metadata": null,
"name": "eq",
"value": {
"kind": "Variable",
"variableName": "id"
}
}
]
} Relay has strict argument rules which only allow simple query arguments like: query AlbumQuery($id: String!) {
- album(id: { eq: $id }) {
+ album(id: $id) {
id
name
} Which you can sort-of get to work, but obviously doesn't allow for the flexibility of Gatsby's query filtering/sorting. After lots of digging around and trying out different tools and libraries like My config in {
"apollo": {
"schemas": {
"gatsby": {
"endpoint": "http://localhost:8000/___graphql"
}
},
"queries": {
"schema": "gatsby",
"includes": ["./src/**/*.js"],
"excludes": ["node_modules/**"]
}
}
} I then run apollo codegen:generate --tagName=graphql --target=flow __generated__ Which will generate flow nice, flat flow type definitions alongside each file containing a graphql query/fragment. Hope that saves somebody some time. 👌 Next task I'm trying to figure out is to get it to run programatically after the server starts up and to watch the files for changes to generate new types automatically. |
@levibuzolic When I follow your steps I get the following error |
Sorry @cameron-martin I'm not using Gatsby at the moment, my guess is there's been a change in a recent version of
|
The correct config seems to now be: {
"apollo": {
"client": {
"service": {
"name": "gatsby",
"url": "http://localhost:8000/___graphql"
}
}
}
} |
Does anyone know how to generate types if your queries use fragments that are defined inside of dependencies? e.g., I have a page template like: import { graphql } from "gatsby";
export const query = graphql`
query pageQuery($id: String!) {
page(id: { eq: $id }) {
id
slug
title
...BodyFragment
}
}
` But I don't know if it's possible to make apollo code gen find and use that fragment when generating types. Currently, it errors out with:
|
For some reason I see a lot of people are still coming from this issue into other issues in Apollo Codegen. |
@Urigo Can gql-gen generate types from string-literal page querys in gatsby? Apollo can do that, and I couldn't find any info on it for gql-gen |
@jakst Yes, it's possible. GraphQL Code Generator looks for In order to start using GraphQL Code Generator:
Then you can use our Wizard tool to guide you through the setup process:
It will ask you couple of questions, one is about the plugins, so please select One of the steps require to setup GraphQL endpoint (schema), I don't know how Gatsby exposes that, maybe through a file or http (http://localhost:8000/___graphql)? When you will be asked about documents, you want to define a glob there. For example, if your fragments and operations are inside of TypeScript files, you do: We would be happy to help you and get your feedback :) There's a still a lot to improve, of course so every suggestion counts! |
Hey @kamilkisiela, thanks for the elaborate answer. I didn't realise I could target .ts files with the glob pattern. But anyway, it does not seem to work. codegen.yaml overwrite: true
schema: 'http://localhost:8000/___graphql'
documents: 'src/**/*.tsx'
generates:
src/queries.ts:
plugins:
- 'typescript-common'
- 'typescript-client' In src/pages/index.tsx I have the following: export const pageQuery = graphql`
query MainPageQuery {
allContentfulPage(filter: { name: { eq: "main" } }) {
edges {
node {
id
title
ingress
}
}
}
}
` It does generate all possible type combinations from the graphql endpoint, but I get no generated type for my pageQuery. |
@jakst which version of graphql-code-generator do you use? |
@jakst @dotansimha I did a little investigation into what's going on with gatsby + graphql-code-generator in dotansimha/graphql-code-generator#1321. TL;DR is that importing |
Great! I'll follow the issue over there to see if there's any progress |
How to setup graphql-code-generator for Gatsby: Worked for me 👍. |
as @panzerdp said, we currently support importing |
@dotansimha any idea why an error related to generated types from graphql-codegen would get thrown just prior to the Gatsby development server starting? Gatsby bootstrapping succeeds but then an error is thrown for the first type in the generated file.
|
@gentleShark It seems like an invalid generated code, but it's hard to point the actual issue. |
Hi @dotansimha - it was a case of user error. I was missing @graphql-codegen/typescript in my devDependencies. up and running now! thanks! |
@panzerdp @dotansimha how does graphql-codegen know what types to generate for Gatsby-image fragments? does it work fine with Gatsby images or only simpler queries? |
I see thanks @panzerdp Just not totally sure to understand how graphql-codegen find what to generate when it find sharp fragments? If I understand correctly, it's this config that enables it to find the fragments right? You should maybe add in this doc page that codegen support those sharp fragments ;) read the doc but it was not totally obvious to me it would work: https://graphql-code-generator.com/docs/integrations/gatsby |
@slorber, |
Thanks, don't think it's related because media is optional. Actually I think you also have the issue @panzerdp it's just you did not use TS in strict mode (you have Not really related but you define your own types for Fluid images: https://github.com/panzerdp/dmitripavlutin.com/blob/master/src/typings/image.d.ts#L1 |
If it's useful to anyone, here's also a solution to generate gatsby TS using apollo cli instead of graphql codegen: |
The text was updated successfully, but these errors were encountered: