【问题标题】:GraphQL Stitching - Why would fields from subschemas return null?GraphQL Stitching - 为什么子模式中的字段会返回 null?
【发布时间】:2021-04-10 07:37:32
【问题描述】:

我正在尝试将两个 GraphQL 模式拼接在一起,一个来自 contentful,另一个来自 neo4j。

每个子模式似乎在跨组合模式的查询期间被询问,但“外来”字段总是返回为空。

我就是想不通这个。

示例查询:

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, 
  }
}

结果:

  "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! :(
    }
  }

日志记录:

//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"
}

这似乎表明某些东西正在起作用,但重量的结果从未进入最终输出。 ApolloServer 不会通过 didEncounterErrors() 报告任何错误

拼接:

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

            }
        }
    ],
})

架构:

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,

});

服务器:

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

非常感谢任何见解或想法!

【问题讨论】:

    标签: graphql schema apollo-server contentful neo4j-graphql-js


    【解决方案1】:

    这似乎是因为 ApolloServer 不支持stitchSchemas...

    Does Apollo Server work with GraphQL Tools stitchSchemas?

    【讨论】:

    • 我们需要更多的调试。比如,模式本身是什么?类型真的匹配吗?您使用的是什么版本的软件包?这绝对不是 Apollo Server 的问题,因为您的服务器不关心缝合模式的内部结构,甚至根本不知道它是缝合的。
    猜你喜欢
    • 2020-03-01
    相关资源
    最近更新 更多