Comments (3)
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.
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.
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)
- FieldResolver performance HOT 2
- Inline fragment from an interface to an interface that extends it is invalid
- Resource exhaustion exploit when parsing queries HOT 7
- In GraphQL how to set WriteOnce functionality in specific property in the collection schema HOT 1
- Generator generates alias imports not compatible with TypeScript Type-Only Imports and verbatimModuleSyntax HOT 2
- Printing the @oneOf directive HOT 1
- Allow shallow sorting with lexicographicSortSchema HOT 2
- Error after upgrading from 16.6 to 16.8.1 with globalThis.process.env.NODE_ENV HOT 12
- Add: Link to new api docs in the README of this project.
- Introspection result missing interfaces
- Detect subscription successfully started HOT 1
- Errors thrown when iterating over subscription source event streams (AsyncIterables) should be caught HOT 2
- graphql resolver circular dependency type error (bi-directional relation) HOT 1
- Current main is up to 50% slower than previous major version HOT 10
- Type definition for `DefinitionNode` appears to be wrong HOT 4
- AST: `IntValueNode` and `FloatValueNode` store values as string HOT 2
- Array-type resolvers: an intuitive solution to the n+1 problem HOT 1
- Performance of stack traces in errors results in high response latency (>1 second) HOT 1
- Support for deep input graphs HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-js.