Advertisement
Guest User

AddSchema

a guest
Nov 27th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const {
  3.     RenameTypes,
  4.     transformSchema,
  5.     introspectSchema,
  6.     RenameRootFields,
  7.     makeRemoteExecutableSchema
  8. } = require('graphql-tools')
  9. const fetch = require('node-fetch')
  10. const { HttpLink } = require('apollo-link-http')
  11.  
  12. module.exports = async function ({
  13.     addSchema, // gridsome api { addSchema }
  14.     prefix, // new schema prefix
  15.     uri // graphql source url
  16. }) {
  17.  
  18.     const link = new HttpLink({
  19.         uri,
  20.         fetch,
  21.         // headers: {}
  22.     })
  23.  
  24.     const schema = await introspectSchema(link)
  25.  
  26.     const executableSchema = await makeRemoteExecutableSchema({ schema, link })
  27.  
  28.     const transformedSchema = transformSchema(
  29.         executableSchema, [
  30.             new RenameTypes( (type) => `${prefix}${type}` ),
  31.             new RenameRootFields( (operation, name) => `${prefix}${name}` )
  32.         ]
  33.     )
  34.  
  35.     // Stitch transformed schema
  36.     addSchema(transformedSchema)
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement