Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
¿Cómo importar esquemas y resolutores en graphql-yoga/node?

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?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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();
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!