Skip to content
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

Support for multiple schemas (on multiple endpoints) #84

Closed
estaub opened this issue Jun 7, 2017 · 13 comments
Closed

Support for multiple schemas (on multiple endpoints) #84

estaub opened this issue Jun 7, 2017 · 13 comments
Labels

Comments

@estaub
Copy link

estaub commented Jun 7, 2017

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.

@jimkyndemeyer
Copy link
Collaborator

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.

@hsyed
Copy link

hsyed commented Jun 7, 2017

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.

@yuricamara
Copy link

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!');
});

@Epenance
Copy link

Epenance commented Nov 3, 2017

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.

@laukaichung
Copy link

laukaichung commented Mar 26, 2018

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.

@ph55
Copy link

ph55 commented May 14, 2018

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

@jimkyndemeyer
Copy link
Collaborator

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.

@jimkyndemeyer
Copy link
Collaborator

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.

@jimkyndemeyer
Copy link
Collaborator

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

@petr-ujezdsky
Copy link

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

api1
└── 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.

@jimkyndemeyer
Copy link
Collaborator

jimkyndemeyer commented Jul 27, 2020

@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).

@Blindpupil
Copy link

For sometime I had it working like this and it would generate a core.schema and content.schema correctly, but after moving to the new format with .yml it doesn't work anymore.

{
  "name": "MyApp GraphQL Schema",
  "extensions": {
    "endpoints": {
      "schemaPath": "core.graphql",
      "core": {
        "url": "http://localhost:4000/q",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": true
      },
      "content": {
        "schemaPath": "content.graphql",
        "url": "http://localhost:3000/content/q",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  },
  "projects": {
    "myApp": {
      "schema": ["core.graphql", "content.graphql"]
    }
  }
}

This is the new grahpql.config.yml that doesn't work:

projects:
  myApp:
extensions:
  endpoints:
    core:
      schemaPath: core.graphql
      url: http://localhost:4000/q
      headers:
        user-agent: JS GraphQL
      introspect: true
    content:
      schemaPath: content.graphql
      url: http://localhost:3000/content/q
      headers:
        user-agent: JS GraphQL
      introspect: true
schema: [core.graphql, content.graphql]

@jimkyndemeyer
Copy link
Collaborator

@Blindpupil Please log a new issue which includes the version of the plugin you're using. This is a very old thread for 1.x of the plugin. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants