0

Having trouble figuring out how/where to get subschema for delegateToSchema.

I have 2 schemas in separate files which I am merging using mergeTypeDefs:

# booking schema
type Booking {
 id
 name
}

type Query {
 booking(id: Int!): Booking
}

and the other schema:

# user schema
type User {
 id: Int!
 name: String!
 bookingId: Int!
 booking: Booking
}

type Query {
 users: [User]
}

I want to use delegateToSchema so I don't have to resolve booking field manually for my User type, so I was trying this in my resolvers:

export const resolvers = {
  Query: { ... }
  User: {
    booking: (parent, args, context, info) => delegateToSchema({
      schema: subschema // <<< how do I get this? 
      operation: 'query',
      fieldName: 'booking',
      args: { id: parent.booking_id },
      context,
      info
    })
  }
}

I have tried to use loadSchema but no luck. NOTE: all 3 are in separate files

Lin Du
  • 88,126
  • 95
  • 281
  • 483
aiiwa
  • 591
  • 7
  • 27

1 Answers1

0

You must be creating the schema somewhere, no? You're either defining it directly (as here):

let postSchema = makeExecutableSchema({
  typeDefs: /* GraphQL */ `
    type Post {
      id: ID!
      text: String
      userId: ID!
    }

    type Query {
      postById(id: ID!): Post
      postsByUserId(userId: ID!): [Post!]!
    }
  `
})

Or you're loading it from a remote server, as here:

const executor: AsyncExecutor = async ({ document, variables }) => {
  const query = print(document)
  const fetchResult = await fetch('http://example.com/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ query, variables })
  })
  return fetchResult.json()
}

export default async () => {
  const schema = wrapSchema({
    schema: await introspectSchema(executor),
    executor
  })
  return schema
}

It's the await introspectSchema(executor) that returns the schema in this case.

If you're not doing either of these...well, you probably could, right?

user3810626
  • 6,798
  • 3
  • 14
  • 19