Dado que graphql-yoga V1 ya no es compatible, me gustaría cambiar a graphql-yoga/node V2.
Estudié la documentación oficial en el sitio web, pero tengo problemas para migrar de V1 a V2.
¿Se requiere un paquete de terceros?
aquí hay un código básico:
const server = createServer({ schema: `type Query { me: User! posts(query: String): [Post!]! users(query: String): [User!]! comments(query: String): [Comment!]! }`, resolvers:{ Query: { posts(parent, args, ctx, info) { if (!args.query) { return posts; } return posts.filter((post) => { const isTitleMatch = post.title .toLowerCase() .includes(args.query.toLowerCase()); const isBodyMatch = post.body .toLowerCase() .includes(args.query.toLowerCase()); return isTitleMatch || isBodyMatch; }); } } } })Como puede ver, tengo resolutores y esquemas, ambos están en un solo archivo llamado server.js
¿Podría alguien por favor ayudarme en esta situación?
De acuerdo con los documentos , debería ser:
const server = createServer({ schema: { typeDefs: `type Query { me: User! posts(query: String): [Post!]! users(query: String): [User!]! comments(query: String): [Comment!]! }`, resolvers: { Query: { posts(parent, args, ctx, info) { if (!args.query) { return posts; } return posts.filter((post) => { const isTitleMatch = post.title .toLowerCase() .includes(args.query.toLowerCase()); const isBodyMatch = post.body .toLowerCase() .includes(args.query.toLowerCase()); return isTitleMatch || isBodyMatch; }); } } } } }) de todos modos, aquí hay un ejemplo de configuración básica con typedefs y resolver en archivos externos. tenga en cuenta que utiliza graphql-tools graphql para cargar el archivo .graphql para el esquema, pero puede usar fácilmente el mismo método que los resolutores para el esquema como archivo .js :
import { createServer } from '@graphql-yoga/node'; import { resolvers } from './resolvers.js'; import { makeExecutableSchema } from '@graphql-tools/schema'; import { loadFiles } from '@graphql-tools/load-files'; const getSchema = async () => makeExecutableSchema({ typeDefs: await loadFiles('./*.graphql'), resolvers, }); async function main() { const schema = await getSchema(); const server = createServer({ schema }); await server.start(); } main();