Importing GraphQL schema from JS file

I have imported my GraphQL schema already using the Fauna dashboard; however, I now made changes to my local schema.gql file and would like to import and merge this new updated local schema and overwrite the remote schema in FaunaDB.

I understand I must use this endpoint https://graphql.fauna.com/import for importing my local schema, but I am not sure how to do so.

For example, using ApolloServer, I know that I can connect the the Fauna’s graphql endpoint to make queries on my schema already present on my FaunaDB:

import  { ApolloServer, makeRemoteExecutableSchema, introspectSchema } from 'apollo-server-micro'
import { HttpLink } from 'apollo-link-http'
import fetch from 'node-fetch'

const link = HttpLink({
  uri:  {
    "https://graphql.fauna.com/graphql", fetch
  },
  headers: {
    Authorization: `Bearer ${process.env.SERVER_KEY}`
  }
})

const schema = makeRemoteExecutableSchema({
  schema: introspectSchema(link),
  link
})

const server = new ApolloServer({
  schema,
  introspection: true
})

export default server.createHandler({
  path: 'api/apolloServer',
  cors: {
    origin: "*",
    credentials: true
  }
})

How do I import my schema in a similar manner using this with the https://graphql.fauna.com/import endpoint?

Hi @platocrat and welcome!
Did you take a look here?
There is general instruction on how to overwrite/merge your GraphQL schema.

Hope this helps.

Luigi

@Luigi_Servini I’ve already viewed this documentation, hence where I pulled my URLs from.

What I’m asking for is specific information not provided in the documentation.

For example, if I want to import my schema and override my pervious schema, I know I must use the following query format:

However, I want to do this in javascript. I want to avoid using the Fauna dashboard to save time

I like using the got npm package to post the schema.

const importSchema = async (faunadbKey, schema) => {
  try {
    const response = await got.post(
      `https://graphql.fauna.com/import`,
      {
        body: schema,
        headers: {
          Authorization: `Bearer ${faunadbKey}`,
        },
      }
    )

    if (response.statusCode !== 200) {
      throw new Error(response.body)
    }

    return response.body
  } catch (error) {
    return error.response.body
  }
}

I’ve wrapped that in a package that you can use, or just copy the code from:

2 Likes

Thank you!
I’ll try this out tomorrow and see if it works.

I also was working on my own implementation that I haven’t tested yet, although, I’m not particularly optimistic about it.

1 Like