Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Accessing @inContext directive when stitching schemas

Accessing @inContext directive when stitching schemas

BoilerRoom
Visitor
2 0 0

I'm having a little trouble accessing the `inContext` directive after stitching the Shopify shopfront API schema with my local schema. 

const {makeExecutableSchema} = require('@graphql-tools/schema')
const {stitchSchemas} = require('@graphql-tools/stitch')
const {buildHTTPExecutor} = require('@graphql-tools/executor-http')
const {schemaFromExecutor, RenameTypes, RenameRootFields} = require('@graphql-tools/wrap')
const env = require('./../env')

const typeDefs = require('./types')
const resolvers = require('./resolvers')

const boilerRoomSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
  logger: console,
})

const shopifyRemoteExecutor = buildHTTPExecutor({
  endpoint: 'https://REDACTED.myshopify.com/api/2022-10/graphql.json',
  headers: {
    'X-Shopify-Storefront-Access-Token': env.SHOPIFY_ACCESS_TOKEN,
  },
})

const createShopifySchema = async () => ({
  schema: await schemaFromExecutor(shopifyRemoteExecutor),
  executor: shopifyRemoteExecutor,
  transforms: [
    new RenameTypes(name => `shopify_${name}`),
    new RenameRootFields((operationName, fieldName, fieldConfig) => `shopify_${fieldName}`),
  ],
})

const createSchema = async () => {
  const shopifySchema = await createShopifySchema()

  return stitchSchemas({
    subschemas: [
      boilerRoomSchema,
      shopifySchema,
    ],
  })
}

module.exports = createSchema
Replies 2 (2)

Liam
Community Manager
3108 344 904

 

Hi BoilerRoom,

 

Directives are not included by default when fetching a remote schema because the introspection query used to fetch the schema doesn't include them. You might need to manually add the inContext directive to your local schema.

 

Here is an example of can define it in your local schema:

directive @inContext(
  country: String
) on FIELD

type Query {
  products: [Product] @inContext(country "CA")
  // ... other fields
}

// ... other types

Try this out and let us know if you're still seeing issues!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

BoilerRoom
Visitor
2 0 0

By adding `mergeDirectives: true` to `stitchSchemas` we can see the `inContext` directive when querying for directives, however it doesn't seem to be being passed to the Shopify stitched schema queries.