graphql-code-generator | generating code based on a GraphQL schema | GraphQL library
kandi X-RAY | graphql-code-generator Summary
kandi X-RAY | graphql-code-generator Summary
GraphQL Code Generator is a tool that generates code out of your GraphQL schema. Whether you are developing a frontend or backend, you can utilize GraphQL Code Generator to generate output from your GraphQL Schema and GraphQL Documents (query/mutation/subscription/fragment). By analyzing the schema and documents and parsing it, GraphQL Code Generator can output code at a wide variety of formats, based on pre-defined templates or based on custom user-defined ones. Regardless of the language that you're using, GraphQL Code Generator got you covered. GraphQL Code Generator lets you choose the output that you need, based on plugins, which are very flexible and customizable. You can also write your plugins to generate custom outputs that match your needs. You can try this tool live on your browser and see some useful examples. Check out GraphQL Code Generator Live Examples. We currently support and maintain these plugins (TypeScript, Flow, React, Angular, MongoDB, Stencil, Reason, and some more), and there is an active community that writes and maintains custom plugins.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of graphql-code-generator
graphql-code-generator Key Features
graphql-code-generator Examples and Code Snippets
Community Discussions
Trending Discussions on graphql-code-generator
QUESTION
I faced with an issue that can't resolve on my own. Let's go through it step by step to point out the problem.
- I have a mutation
bookAppointment
which returns anAppointment
object - GraphQL schema says that this object should return 4 properties:
id
,date
,specialist
,client
. - To follow the GraphQL-style the
specialist
andclient
properties should be a field level resolvers - To fetch this objects I need pass
specialistId
to the specialist field level resolver, as well asclientId
to the client field level resolver. - At this point a problem arises.
- The field level resolvers of
client
,specialist
expects that root mutation returns fields likeclientId
andspecialistId
. But GraphQL syntax and types that were generated by that syntax doesn't include this props (make sense). - How to "extend" the return type of the resolver and its
interface BookAppointmentPayload
to make me and TypeScript happy?
This is my GraphQL schema
...ANSWER
Answered 2022-Feb-04 at 20:57I've been investigating this problem quite a lot and have come to the following conclusion.
Create an interface which represent "actual" return type of the resolver
Most of the time the return type of the resolver function (in JavaScript) doesn't match the type that was declared in the GraphQL SDL
For instance,
QUESTION
A custom scalar type named Date
which is an ISO 8601 string is defined in the backend.
In the frontend "GraphQL Code Generator" (https://www.graphql-code-generator.com/) is used to generate typescript types from the schema.
The codegen.yml
looks like this:
ANSWER
Answered 2021-Oct-03 at 02:33Codegen doesn't do anything for conversion, you could put Date: number
and it would happily print that in your generated file. It is really just a way to automatically generate Typescript types for your API.
What you are looking for is different, you basically need a "middleware" from you graphql client that:
- Has access to the schema. So this means you need to be comfortable shipping your whole schema with your frontend application, which most developers are not especially for private APIs.
- Can parse said schema to match fields in the JSON response to scalars and then interpret them into whatever native type your want (like
Date
)
For the Apollo client, you can use https://github.com/eturino/apollo-link-scalars. For urql, I found https://github.com/clentfort/urql-custom-scalars-exchange.
QUESTION
I am struggling to understand how to use Apollo Rover in conjunction with Apollo Codegen to generate my typescript types for my API's schema. I registered my schema in apollo studio, and can grab the schema into a GQL file, but it looks like codegen is not supported in the Rover CLI and so I need to use the legacy Apollo CLI. However the Apollo CLI wants schema to be fetched from a remote endpoint and not a registered schema, and wants the schema in JSON format, but Rover only fetches in .gql format.
I'm confused about how I am "supposed" to hook this up so that I can use apollo codegen with my registered schema without a bunch of manual conversion work.
...ANSWER
Answered 2021-Sep-26 at 16:51I found this. I think you should keep use Apollo CLI.
Rover does not currently provide client-specific features, such as code generation or client checks. For these features, continue using the Apollo CLI.
Another solution use, it work for me ;)
QUESTION
GraphQL Code Generator creates this type on the top of the created TypeScript file:
...ANSWER
Answered 2021-Sep-21 at 14:14It aims to make it so that one cannot pass an object with any additional properties (in addition to id
) as FooQueryVariables
. But it fails to do so: https://github.com/dotansimha/graphql-code-generator/issues/4577
QUESTION
So far I understand I need to build my own baseQuery
. I could write graphql queries and mutations like in example here https://rtk-query-docs.netlify.app/examples/react-with-graphql, will I get full type safety for queries and mutations if I add types to query.builder
like this builder.query
or I must use something like this https://www.graphql-code-generator.com/docs/plugins/typescript-graphql-request#simple-request-middleware. In latter case how should my baseQuery
look if I use generated hook for graphql-request library.
Here is example of hook from 2:
...ANSWER
Answered 2021-Jun-03 at 16:03Actually I started writing a plugin for the code generator a few days ago. You can see the generated result here: https://github.com/phryneas/graphql-code-generator/blob/5f9a2eefd81538782b791e0cc5df633935164a89/dev-test/githunt/types.rtk-query.ts#L406-L427
This would require you to create an api with a baseQuery using a graphql library of your choice like this.
QUESTION
I have a Gatsby page component and try to query some data from GraphCMS. I added the page component inside a folder:
...ANSWER
Answered 2021-May-14 at 04:59Your allGraphCmsProject
information it's inside props.data.allGraphCmsProject
so the resultant structure should be:
QUESTION
I've got a bunch of generated types (via GraphQL codegen) that look like this:
...ANSWER
Answered 2021-Feb-08 at 17:10It's a bit tricky to tease union members apart from each other, and I'm not 100% sure about the general use case, but here's one way you might approach it:
QUESTION
I have my AppSync api set up using aws-cdk and am not using their amplify framework. I am trying to figure out how / if I can generate Typescript definitions from my AppSync schema.graphql
file while not using amplify, i.e. no access to amplify codegen
command. I did try installing and running it, but I assume amplify expects files to be located in certain directories, hence failing.
I looked into https://graphql-code-generator.com but it wont work due to special types AppSync uses like AWSDateTime
, a work around for this is to have api published and get schema from a graphql endpoint, but this is not ideal i.e. I'd like to be able and generate these types locally without publishing the schema.
Is this doable?
...ANSWER
Answered 2020-Dec-17 at 15:40If there's some custom scalars (like AWSDateTime
) that aren't part of your schema.graphql
file, you can just create a separate file like scalars.graphql
and add those missing type definitions yourself:
QUESTION
We have a graphql query that is executed like this:
...ANSWER
Answered 2020-Dec-01 at 13:52Figured it out. we only needed to check the __typename
property before returning the object data 'array':
QUESTION
I am wondering if arrays defined in graphql queries/mutations, which can be either singletons or tuples in valid graphql implementation get converted to strict array types by graphql-codegen. Is this is by design ?
Given this schema:
...ANSWER
Answered 2020-Nov-10 at 18:03GraphQL inputs in operations supports coercion, but codegen doesn't (see: https://github.com/dotansimha/graphql-code-generator/issues/4888) We are working on it and hope to fix that in upcoming versions ;)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install graphql-code-generator
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page