UNPKG

@graphql-hive/federation-gateway-audit

Version:
201 lines (188 loc) 3.54 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const authors = [ { id: "a1", name: "John", yearsOfExperience: 5 }, { id: "a2", name: "Jane", yearsOfExperience: 20 } ]; const posts = [ { id: "p1", body: "p1-body", author: authors[0] }, { id: "p2", body: "p2-body", author: authors[1] } ]; var a = testkit.createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@requires"] ) type Query { feed: [Post] } type Post @key(fields: "id") { id: ID! byNovice: Boolean! @external byExpert: Boolean! @requires(fields: "byNovice") } type Author @key(fields: "id") { id: ID! name: String! yearsOfExperience: Int! } ` ), resolvers: { Query: { feed() { return posts.map((post) => ({ id: post.id })); } }, Post: { __resolveReference(ref) { const post = posts.find((post2) => post2.id === ref.id); if (!post) { return null; } if (ref.byNovice == null) { return { id: post.id }; } return { id: post.id, byNovice: ref.byNovice }; }, byExpert(post) { if (post.byNovice == null) { return null; } return !post.byNovice; } }, Author: { __resolveReference(ref) { const author = authors.find((author2) => author2.id === ref.id); if (!author) { return null; } return { id: author.id, name: author.name, yearsOfExperience: author.yearsOfExperience }; } } } }); var b = testkit.createSubgraph("b", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@requires"] ) type Post @key(fields: "id") { id: ID! author: Author! byNovice: Boolean! @requires(fields: "author { yearsOfExperience }") } type Author @key(fields: "id") { id: ID! yearsOfExperience: Int! @external } ` ), resolvers: { Post: { __resolveReference(ref) { const post = posts.find((post2) => post2.id === ref.id); if (!post) { return null; } return { id: post.id, author: { id: post.author.id, ...ref.author } }; }, byNovice(post) { return post.author.yearsOfExperience < 10; } } } }); var test = [ testkit.createTest( /* GraphQL */ ` { feed { byNovice } } `, { data: { feed: [ { byNovice: true }, { byNovice: false } ] } } ), testkit.createTest( /* GraphQL */ ` { feed { byExpert } } `, { data: { feed: [ { byExpert: false }, { byExpert: true } ] } } ) ]; var index = testkit.serve("requires-circular", [a, b], test); exports.default = index;