Js-graphql-intellij-plugin: Support for multiple schemas (on multiple endpoints)

Created on 7 Jun 2017  ·  11Comments  ·  Source: jimkyndemeyer/js-graphql-intellij-plugin

I need to access two different graphql endpoints from a project, where each endpoint has a different schema.

I'm currently keeping two different copies of graphql.config.json that I swap between as needed.

Is there a better way to handle this? If not, consider this an enhancement request.

enhancement v2-alpha v2-architecture

Most helpful comment

The 2.0 alpha makes it possible to have multiple schemas in a single project using IntelliJ/WebStorm "Scopes". You can configure them for the project in the IDE Settings dialog.

See 2.0.0-alpha-2 if you'd like to try it and help test it.

All 11 comments

There's no support for this at the moment. I suggest you have two scripts that copy to graphql.confg.json which is watched by the plugin.

I just hit this one as well, working on a storage backend that has multiple schemas. It would be very useful to pair the schema with the endpoint in some way.

Has anyone tried merging with lodash or something else?

I wrote this code but it doesn't merge as expected.

'use strict';
const { merge } = require('lodash');

const fs = require('fs');

const schemasClosed = require('../src/graphql/schemas/schemas.closed.json');
const schemasOpened = require('../src/graphql/schemas/schemas.opened.json');
const schemasRestricted = require('../src/graphql/schemas/schemas.restricted.json');

const schemas = merge(schemasClosed, schemasOpened, schemasRestricted);
const schemasToString = JSON.stringify(schemas);

fs.writeFile('src/graphql/schemas/schemas.json', schemasToString, (err) => {
  if (err) throw err;
  console.log('The file has been saved!');
});

Would indeed be amazing to have, I have several endpoints, that i would love to have support for rather than having to update my config all the time when i change between them.

I've already set to get the schema from an url endpoint. I don't know how to make the plugin to get a .graphql schema file for apollo link state's local store as well as from an url endpoint.

Same here.
Working with mono-repository approach.
So the schema is different per endpoint.

My thoughts about architecture:

  • Schema could be set per endpoint.
    image
  • graphql.config.json could be placed in many directories in project.
    image

The 2.0 alpha makes it possible to have multiple schemas in a single project using IntelliJ/WebStorm "Scopes". You can configure them for the project in the IDE Settings dialog.

See 2.0.0-alpha-2 if you'd like to try it and help test it.

I'll quickly outline how to set it up.

The schemas are kept apart using the "Scopes" feature in your IDE:

image

For this example, I've added a single scope, and clicked the "src" folder, followed by "Include recursively".

As you work with GraphQL in the editor, the file you're in will be matched against the scopes, and the first match limits which schema type definitions etc. that are included.

Hope that clears things up.

I plan on creating a GraphQL-specific dialog for scopes such that users are free to use these generic scopes for other use cases.

See https://github.com/jimkyndemeyer/graphql-config-examples for examples of how to configure multiple schemas with v2 of the plugin.

Hi,

I am unable to "link" 4 projects together properly.
@jimkyndemeyer Could you please help me with the proper configuration?
My project looks like this

└── src
    └── main
        └── resources
            ├── .graphqlconfig
            └── mySchema.graphqls

api2
└── src
    └── main
        └── resources
            ├── .graphqlconfig
            └── mySchema.graphqls

application1
└── src
    └── myComponent.jsx

application2
└── src
    └── myComponent.jsx

Where application1 uses api1 and application2 uses api2.
Both api run at different ports.

.graphqlconfig for api1:

{
  "name": "Api1 GraphQL Schema",
  "schemaPath": "mySchema.graphql",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://localhost:8081/graphql",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  }
}

.graphqlconfig for api2:

{
  "name": "Api2 GraphQL Schema",
  "schemaPath": "mySchema.graphql",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://localhost:8082/graphql",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  }
}

The "link" between application1 and api1 works flawlessly. I can click-through gql queries and their containing types from myComponent.jsx into myScheme.graphql. It also autocompletes fields inside queries.
However in application2 all queries and types are "linked" to api1 schema - when I click-through query / contained type I am navigated into schema inside api1 instead of inside api2.
Strange thing is that query defined only inside api2 is recognised properly and I am navigated to it. But Idea has no autocomplete options inside this query and every field is red.

I have looked at the projects section of .graphqlconfig but I do not know how to "link" them to their related application.
I tried to create only one .graphqlconfig in project root (with just one endpoint for now):

{
  "name": "Apis GraphQL Schema",
  "projects": {
    "api1": {
      "schemaPath": "api1/src/main/resources/mySchema.graphqls",
      "includes": ["api1/**", "application1/**"]
    },
    "api2": {
      "schemaPath": "api2/src/main/resources/mySchema.graphqls",
      "includes": ["api2/**", "application2/**"]
    }
  },
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://sokol.local:8082/graphql",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  }
}

But Idea did not recognise that. Idea expects to have this file in resources folder as I currently have. It even suggests to create new one there while you click on the wrench icon on top of mySchema.graphqls.

@petr-ujezdsky You're on the right track with the projects-based configuration since your schemas span folders at the same levels in the file system. I'm not sure what you mean by the resources folder, but the graphql config needs to be in the same or a parent directory to anything that it matches in the includes. For each file, looking for a config in the current directory and any parent directories is how each file is then associated back to the config, meaning that it logically belongs to that project schema.

The example at https://github.com/jimkyndemeyer/graphql-config-examples/blob/master/two-schemas-plus-shared-using-projects is pretty close to what you're trying to do. Maybe there's a clue there as to why your config isn't working.

Edit: Take a look in the "File" > "Project structure" > "Modules" dialog of your project. The root directory that you place the config file in must be considered part of the project files to be picked up by the plugin (it uses the "Project files" search scope).

Was this page helpful?
0 / 5 - 0 ratings