A
A
Andrey Majidov2019-06-04 19:55:23
Node.js
Andrey Majidov, 2019-06-04 19:55:23

Is there a Node.js API to generate a GraphQL schema file on the fly?

I am writing an application with Node.js backend and GraphQL API.
I use Nodemon to restart the server on code changes.
I describe the GraphQL schema in a standard programmatic way of the form:

const UserQuery = {
  type: GraphQLUser,
  args: {
    id: { type: GraphQLString }
  },
  resolve: async (root, { id }, context) => {
    if (id) return await User.findById(fromGlobalId(id).id)
    return context.request.user
  }
}

At the moment, to generate a text file with a schema (schema.graphql), I use a plugin for WebStorm, which sends an Introspection query to the graphql endpoint and generates a schema.graphql file based on the response.
After every change in schema js files, I have to call this plugin action manually to generate the actual schema.
Q:
Is there any npm API package or method in the graphql package itself that does the same thing when called? That is, generating a text file of the scheme.
It would be convenient to perform this action during server startup, which, together with Nodemon, would eliminate manual clicks on the plugin in the IDE, since when changes were made to the js-files of the schema, the server would be restarted and the actual schema would be generated.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2019-06-04
@Famence

can be generated programmatically at startup if you have schema in memory:
https://blog.apollographql.com/three-ways-to-repre...
See section "Converting between all of the different types"
You can also save schema.graphql and schema.json.
You can make a watcher in the IDE so that when you change the necessary files, it launches a script that will load your scheme and export it to a file.
And other perversions to your taste.
Another question - why do you need it at all, it's easier to write everything in * .graphql files or in ts with the gql tag and import them directly, and use them to work with all sorts of tools / plugins and other things.

I
Ivan Shumov, 2019-06-04
@inoise

for this, blue-green deployment is usually used. a new project instance is launched and then traffic is switched to it. And you are trying to solve this problem as a typical developer on your own

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question