0

I'm attempting to stitch together two GraphQL schemas, one from contentful and one from neo4j.

Each subschema appears to be interrogated during queries across the combined schema but "foreign" fields always come back as null.

I just can't figure this one out.

Sample Query:

query {
  //Request data exclusively from the neo4j schema
  Product(id:"475e006f-b9cf-4f40-8712-271ceb46d14b"){
    id,
    name,
    weight
  },
  //This is a contentful schema query which should return weight from neo4j
  product(id:"[contentful-native-id]"){
      id,
      weight, 
  }
}

Result:

  "data": {
    "Product": [
      {
        "id": "475e006f-b9cf-4f40-8712-271ceb46d14b",
        "name": "Test product name",
        "weight": 14.9
      }
    ],
    "product": {
      "id": "475e006f-b9cf-4f40-8712-271ceb46d14b",
      "weight": null //This shouldn't be null! :(
    }
  }

Logging:

//First query being executed against neo4j database
neo4j-graphql-js MATCH (`product`:`Product` {id:$id}) RETURN `product` { .id , .name , .weight } AS `product`
neo4j-graphql-js {
   "offset": 0,
   "first": -1,
   "id": "475e006f-b9cf-4f40-8712-271ceb46d14b"
 }

//Triggered by the second query correctly trying to resolve weight from neo4j
neo4j-graphql-js MATCH (`product`:`Product` {id:$id}) RETURN `product` { .weight , .id } AS `product`
neo4j-graphql-js {
   "offset": 0,
   "first": -1,
   "id": "475e006f-b9cf-4f40-8712-271ceb46d14b"
}

This seems to suggest something is working, but the result of weight never makes it to the final output. ApolloServer doesn't report any errors via didEncounterErrors()

Stitching:

const gatewaySchema = stitchSchemas({
    subschemas: [{
            schema: neoSchema,
            merge: {
                Product: {
                    selectionSet: '{id}',
                    fieldName: 'Product',
                    args: ({
                        id
                    }) => ({
                        id
                    }),
                }
            }
        },
        {
            schema: contentfulSchema,
            merge: {

            }
        }
    ],
})

Schemas:

const executor = async ({
    document,
    variables,
    context
}) => {
    const query = print(document);
    //console.log(query);
    const fetchResult = await fetch('https://graphql.contentful.com/content/v1/spaces/[SPACE]', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer [AUTHTOKEN]`,
        },
        body: JSON.stringify({
            query,
            variables
        })
    });
    return fetchResult.json();
};

const contentfulSchema = wrapSchema({
    schema: await introspectSchema(executor),
    executor: executor
});

const driver = neo4j.driver(
    process.env.NEO4J_URI || 'bolt://localhost:7687',
    neo4j.auth.basic(
        process.env.NEO4J_USER,
        process.env.NEO4J_PASS
    ), {
        encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
    }
)

const neoSchema = makeAugmentedSchema({
    typeDefs: typeDefs,

});

Server:

 const server = new ApolloServer({
      schema: gatewaySchema,
      context: ({ req }) => {
        return {
          driver,
          req
        };
      },
      plugins:[
        myPlugin
      ]
    });

Any insight or ideas much appreciated!

Vok
  • 457
  • 5
  • 19

1 Answers1

0

This appears to be down to the fact that stitchSchemas is NOT supported in ApolloServer...

Does Apollo Server work with GraphQL Tools stitchSchemas?

Vok
  • 457
  • 5
  • 19
  • We need more to debug. Like, what are the schema themselves? Do the types truly match? What versions of packages are you using? This is definitely not a problem with Apollo Server as your server does not care about the internals of the stitched schema, does not even know it is stitched at all. – Yaacov Mar 24 '21 at 20:41