0

I'm new to GraphQL so please forgive any if this is a silly question.

I want to organize GraphQL files(schema and query/mutation files) in client side, which has to be aligned with the GraphQL files on server.

What is the good practice for this purpose? Is there any tool/toolchain/service, or do we have to maintain those manually(i.e. if a schema get changed on server or client, the counterpart in response update the corresponding schema)?

It would be nice if I can just use automatically-synched .graphql file which has no conflict with the ones in server.

cadenzah
  • 928
  • 3
  • 10
  • 23
  • To keep your client-side GraphQL files in sync with the server, you can use a tool like graphql-codegen or a GraphQL client library like Apollo Client or Relay. It's important to make sure that your client-side schema and operations are always up-to-date with the server-side schema – Kareem Adel Apr 19 '23 at 21:07
  • Unless you need something *now*, in which case: `ln -s server/schema.graphql client/schema.graphql` might work – Mikolaj Figurski Apr 20 '23 at 00:45

2 Answers2

0

A recommended approach to achieve this is by utilizing GraphQL Code Generator, a CLI tool for generating code from your GraphQL schema. If you are working with a monorepo that includes both front-end and back-end code, you can follow these steps:

  • Set up GraphQL CodeGen configuration (more info)
  • Set up a watch mode to automatically update backend and frontend files (more info)

You can also refer to an older question here on StackOverflow that discusses a similar issue.

Lucas Arcanjo
  • 565
  • 4
  • 8
  • It seems your approach is fit for a monorepo project, but mine is frontend-only one, which means the api is on remote place. – cadenzah Apr 21 '23 at 15:33
0

This is my advice.Thanks!

1.you should keep your schema and query/mutation files organized in a separate directory or directories within your project. This makes it easier to manage and maintain these files, especially as your project grows and you have more queries and mutations to handle.

2.One approach to keeping your GraphQL files in sync between the client and server is to use a tool like Apollo Codegen or GraphQL Code Generator. These tools can generate typescript interfaces or classes for your queries and mutations based on your GraphQL schema, making it easier to ensure that your client-side code is in sync with your server-side schema. This approach is especially useful when you have a large number of queries and mutations, as it can save you a lot of time and effort in manually managing the files.

3.Another option is to use a version control system like Git to manage your GraphQL files. By keeping your schema and query/mutation files in version control, you can easily track changes to the files and ensure that they stay in sync between the client and server. However, this approach requires manual intervention to update the files when changes are made to the schema on the server.

唐梦儿
  • 11
  • 2