Code Monkey home page Code Monkey logo

Comments (3)

xonx4l avatar xonx4l commented on April 27, 2024 1

The error message "Cannot read properties of undefined (reading 'name')" indicates that the name argument is undefined within your resolver.In this updated version, we are using destructuring to extract the name argument directly from the arguments object.Additionally, when constructing the GraphQL query in the source variable, we'll ensure that we're properly passing the name as a query parameter

from graphql-js.

xonx4l avatar xonx4l commented on April 27, 2024 1

const requestListener = async (req, res) => {
if (req.method === 'GET' && (req.url === '/' || req.url.startsWith('/graphql'))) {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;

const urlParams = new URLSearchParams(req.url.split('?')[1]);
const name = urlParams.get('name');

const source = `query { greet(name: "${name || 'John'}") }`;
const response = await graphql({
  schema,
  source,
  rootValue,
});
res.end(JSON.stringify(response));

} else {
res.statusCode = 404;
res.end('Not Found');
}
};

from graphql-js.

xonx4l avatar xonx4l commented on April 27, 2024 1

const http = require('http');
const { graphql, buildSchema } = require('graphql');

const schema = buildSchema(type Query { hello: String greet(name: String): String });

const rootValue = {
hello: () => {
return 'Hello, world!';
},
greet: ({ name }) => {
return Hello, ${name || 'Stranger'}!;
},
};

const requestListener = async (req, res) => {
if (req.method === 'GET' && (req.url === '/' || req.url.startsWith('/graphql'))) {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;

const urlParams = new URLSearchParams(req.url.split('?')[1]);
const name = urlParams.get('name');

const source = `query { greet(name: "${name || 'John'}") }`;
const response = await graphql({
  schema,
  source,
  rootValue,
});
res.end(JSON.stringify(response));

} else {
res.statusCode = 404;
res.end('Not Found');
}
};

const server = http.createServer(requestListener);
const PORT = 3000;

server.listen(PORT, () => {
console.log(Server is listening on port ${PORT});
});

from graphql-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.