Skip to content

Instantly share code, notes, and snippets.

@jakedohm
Last active November 27, 2020 20:11
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jakedohm/860f2dadbf00f0d96c8f8d773c476351 to your computer and use it in GitHub Desktop.
Save jakedohm/860f2dadbf00f0d96c8f8d773c476351 to your computer and use it in GitHub Desktop.
const { setContext } = require('apollo-link-context');
const { HttpLink } = require('apollo-link-http');
const { introspectSchema, makeRemoteExecutableSchema } = require('graphql-tools');
const fetch = require('node-fetch');
module.exports = function(api) {
api.createSchema(async function(graphql) {
const http = new HttpLink({
uri: 'http://example.com/api',
fetch
});
const link = setContext((request, previousContext) => ({
headers: {
Authorization: `Bearer JSmxi5ocIhjKGENvgK66TrlqoylAPy8ZeAckiyo-4txKkYhdKSfLlPXLnxoghZm3`
}
})).concat(http);
const schema = await introspectSchema(link);
const executableSchema = await makeRemoteExecutableSchema({
schema: schema,
link
});
return executableSchema;
});
};
@1mursaleen
Copy link

I have a Page type in my remote graphql schema (Wordpress wp-graphql) and gridsome won't allow that schema to import into it because it has it's own Page type. I get
Error: Schema must contain uniquely named types but contains multiple types named "Page".
Kindly help.

@1mursaleen
Copy link

1mursaleen commented Jul 18, 2020

Can either of the content type be removed or renamed?
Or
Can the remote schema being imported be assigned a FieldType or TypeName that somehow resolves this issue?

@1mursaleen
Copy link

I see that Transform can be used to rename the new conflicting type as described in this article (https://www.graphql-tools.com/docs/schema-wrapping/). But I'm unsure where and how to put the code for it in the above gist as I do not understand TypeScript used in the documentation.

@1mursaleen
Copy link

Renaming Types and RootFields section of this article (https://medium.com/hasura/the-ultimate-guide-to-schema-stitching-in-graphql-543ebcb25d42) resolved my issue.

Note: I did not use @gridsome/source-graphql plugin because it has limitations and the graphql query wont work inside api.loadSource() in gridsome.server.js

@stereokai
Copy link

Hi, this looks great but I'm having trouble in the shape of this error:

TypeError: gridsome.server.js: executor is not a function (50:26)

  48 |     })).concat(http);
  49 |
> 50 |     const schema = await introspectSchema(link);
     |                          ^
  51 |     const executableSchema = await makeRemoteExecutableSchema({
  52 |       schema: schema,
  53 |       link

Because introspectSchema expects link to be a function

Have any idea? Thanks :)

@1mursaleen
Copy link

Try passing http variable instead of link variable into introspectSchema(), if you need Authorization header you can do it like this:

    const http = new HttpLink({
      uri: 'http://example.com/api',
      fetch,
      headers: {
        Authorization: `Bearer JSmxi5ocIhjKGENvgK66TrlqoylAPy8ZeAckiyo-4txKkYhdKSfLlPXLnxoghZm3`
      }
    });

PS: executor is just an http context expected by introspectSchema.

@stereokai
Copy link

Thanks, can I use that also in the object passed to makeRemoteExecutableSchema?

@1mursaleen
Copy link

yes

@stereokai
Copy link

Thanks. Also, what does your package.json look like? want to make sure I have the same dependencies as you do

@1mursaleen
Copy link

"dependencies": {
        "@gridsome/source-graphql": "^0.1.0",
        "@gridsome/source-wordpress": "^0.5.3",
        "cheerio": "^1.0.0-rc.3",
        "gridsome": "^0.7.19"
    },

@stereokai
Copy link

So you're consuming apollo-link-context, apollo-link-http, graphql-tools, and node-fetch implicitly?

@stereokai
Copy link

I'm getting this error, Error: You should provide GraphQLDirective to schemaComposer.addDirective(), but recieved: @infer
I posted an issue here: gridsome/gridsome#1279
Any idea?
Thank you :)

@1mursaleen
Copy link

So you're consuming apollo-link-context, apollo-link-http, graphql-tools, and node-fetch implicitly?

As I copied the code from the article mentioned above, I thought of this too. Somehow these packages are already installed by gridsome itself or by either of the two source plugins i`m using.

@stereokai
Copy link

Thanks, I replied in the issue.

@stereokai
Copy link

I ended up removing all the package locks (yarn, npm, pnpm) and doing a fresh reinstall using yarn with these dependencies:

    "@gridsome/source-graphql": "~0.1.0",
    "graphql": "^14.0.0",
    "gridsome": "^0.7.0",

@nicolaisimonsen
Copy link

Renaming Types and RootFields section of this article (https://medium.com/hasura/the-ultimate-guide-to-schema-stitching-in-graphql-543ebcb25d42) resolved my issue.

Note: I did not use @gridsome/source-graphql plugin because it has limitations and the graphql query wont work inside api.loadSource() in gridsome.server.js

Would you mind sharing what you did to make it work? I'm having problems following the article mentioned 😕

@1mursaleen
Copy link

https://pastebin.com/uTwYZBeh
https://pastebin.com/t7JQPaPg

These are the two functions I created and called them from gridsome.server.config
I hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment