gmac / schema-stitching-handbook

Guided examples exploring GraphQL Tools v6+ Schema Stitching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not able to create batchDelegateToSchema with stitching directive SDL

fahadbillah opened this issue · comments

I was following your schema-stitching-handbook and it's a great resource. I was trying to migrate from gateway level stitching to stitching directive SDL but unable to convert batchDelegateToSchema to stitching directive. And after debugging I can see dataloader is throwing error because of the key & value length mismatch. Please check the code below.

// Division schema
const typeDefs = `${stitchingDirectivesTypeDefs}

type Division {
  id: ID
  name: String
  districts: [District]
}

type District {
  id: ID
}

type Query {
  divisions(ids: [ID]): [Division] @merge(
    keyField: "id"
    keyArg: "ids"
  ) 
  _sdl: String!
}`

// division resolvers

const resolvers = {
  Division: {
    districts: (division) => ([{id: division.id}])
  },
  Query: {
    divisions: async (_, { ids }) => {
      if (!ids) {
        return divisions;
      }
      return divisions.filter(division => ids.includes(division.id))
    },
    _sdl: () => typeDefs
  }
}

// ------------------------------------------------------------

// District schema
const typeDefs = `
${stitchingDirectivesTypeDefs}

type District {
  id: ID
  name: String
  divisionId: ID
  division: Division
}

type Division {
  id: ID
}

type Query {
  districts(ids: [ID]): [District] @merge(
    keyField: "id"
    keyArg: "ids"
  )
  _sdl: String!
}`

const resolvers = {
    District: {
      division: (district) => ({id: district.divisionId})
    },
    Query: {
      districts: async (_, { ids }) => {
        if (!ids) {
          return districts;
        }
        return districts.filter(district => ids.includes(district.divisionId))
      },
      _sdl: () => typeDefs
    }
  }

While districts works as expected divisions query throws error in dataloader - > https://github.com/graphql/dataloader/blob/3e62fbe7d42b7ab1ec54818a1491cb0107dd828a/src/index.js#L330

# throws error
query divisions{
  divisions {
    id
    name
    districts {
      id
      name
      divisionId
    }
  }
}

# works fine
query districts{
  districts {
    id
    name
    division {
      id
      name
    }
  }
}

Resolver working as expected and returning batch response but dataloader unable to map key value together due to mismatch length.

Am I doing it wrong? I would love to hear your suggestion about this. Thanks in advance.

I have a repo where you can reproduce the issue -> https://github.com/fahadbillah/stitching-directive

Not sure what you mean by unable to convert batchDelegateToSchema to stitching directive... there should be no direct conversion. If you are using batchDelegateToSchema somewhere then you are presumably doing something custom, at which time you'll need to retain your custom implementations. Stitching directives only work for boilerplate field mappings, at which time the SDL example is about as robust of a working explanation as I can offer.