我有一个带有查询的Express-GraphQL API和一个可在GraphiQL中工作的突变,查询的单元测试有效,但突变的单元测试返回405错误。我的GraphQL模式如下:
type Subject {
type: String
}
type Category {
name: String
}
type SubjectCategories {
subject: Subject
categories: [Category]
}
type Query {
subjectCategories(subjectType: String!): SubjectCategories
}
type Mutation {
addSubjectCategory(subjectType: String! categoryName: String!): SubjectCategories
}
Run Code Online (Sandbox Code Playgroud)
为了简化这个问题,这些方法的实现只是回显:
echoGetSubjectCategory({subjectType}, context) {
const subject = new Subject(subjectType);
const category = new Category("blah");
const retval = {
subject,
categories: [category],
};
return retval;
}
echoMutateSubjectCategory({subjectType, categoryName}, context) {
const subject = new Subject(subjectType);
const category = new Category(categoryName);
const retval = …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试解析一个简单的配方列表,其中包含对食材的引用。
数据布局如下所示:
type Ingredient {
name: String!
amount: Int!
unit: Unit!
recipe: Recipe
}
type Recipe {
id: Int!
name: String!
ingredients: [Ingredient]!
steps: [String]!
pictureUrl: String!
}
Run Code Online (Sandbox Code Playgroud)
据我了解,我的解析器应如下所示:第一个解析食谱,第二个解析食谱中的成分字段。根据我的理解,它可以使用配方提供的参数。在我的配方对象中,该成分由id(int)引用,因此这应该是参数(至少我是这样认为的)。
var root = {
recipe: (argument) => {
return recipeList;
},
Recipe: {
ingredients: (obj, args, context) => {
//resolve ingredients
}
},
Run Code Online (Sandbox Code Playgroud)
这些解析器通过以下方式传递到应用程序:
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
rootValue: root,
}));
Run Code Online (Sandbox Code Playgroud)
但是,我的解析器似乎没有被调用。我希望在查询中即时查询所有成分。
该端点有效,但是一旦我查询成分,"message": "Cannot return null for non-nullable field Ingredient.name.",就会返回此消息的错误。
当尝试在解析器中记录传入的参数时,我可以看到它从未执行过。不幸的是,我找不到像我一样使用express-graphql时如何使用它的示例。
如何在express-graphQL中为嵌套类型编写单独的解析器?
刚刚更新为“ graphql-server-express”版本1.3.0,现在在运行任何突变时出现此错误:
POST body missing. Did you forget use body-parser middleware?
Run Code Online (Sandbox Code Playgroud)
初始化服务器时,我包括“ body-parser”包,所以我不确定这里发生了什么。有任何想法吗?
服务器配置:
//GraphQL Server
graphQLServer.use(
//GRAPHQL Endpoint
`/${settings.public.graphql.endpoint}`,
//Parse JSON
bodyParser.json(),
//authenticate
authenticateRequest,
//GRAPHQL Server
graphqlExpress(req => {
return {
schema: schema,
context: req
}
})
);
Run Code Online (Sandbox Code Playgroud)
请求示例:
curl 'http://localhost:5050/graphql' \
-H 'authorization: Bearer xxxxxxxxxxx.xxxxxxxxxxx' \
-d '{
"query": "mutation { sensorData(sensorValue: \u0027asdasdasdasd \u0027)}",
"variables": {}
}
}'
Run Code Online (Sandbox Code Playgroud) 我正在编写用于在 NodeJS 中登录用户的突变。
它给出错误“必须提供名称”。
这是浏览器 GraphQL 查询:
mutation{
login(username:"dfgdfg",password:"test1234") {
_id,
name{
fname,
lname,
mname
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码
const login = {
type: UserType,
args: {
input:{
name:'Input',
type: new GraphQLNonNull(new GraphQLObjectType(
{
username:{
name:'Username',
type: new GraphQLNonNull(GraphQLString)
},
password:{
name:'Password',
type: new GraphQLNonNull(GraphQLString)
}
}
))
}
},
resolve: async (_, input, context) => {
let errors = [];
return UserModel.findById("5b5c34a52092182f26e92a0b").exec();
}
}
module.exports = login;
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决为什么会出现错误吗?
提前致谢。
我正在制作GraphQL API,在其中我可以通过其ID检索汽车对象或在不提供任何参数的情况下检索所有汽车。
使用下面的代码,我可以通过提供id作为参数来成功检索单个汽车对象。
但是,在我希望得到对象数组的情况下,即当我完全不提供任何参数时,在GraphiQL上没有任何结果。
schema.js
let cars = [
{ name: "Honda", id: "1" },
{ name: "Toyota", id: "2" },
{ name: "BMW", id: "3" }
];
const CarType = new GraphQLObjectType({
name: "Car",
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString }
})
});
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
cars: {
type: CarType,
args: {
id: { type: GraphQLString }
},
resolve(parent, args) {
if (args.id) {
console.log(cars.find(car => …Run Code Online (Sandbox Code Playgroud) 我正在学习 GraphQL 并且是该技术的新手。我无法找出此语法错误的原因。当我在 graphiql 上对其进行测试时,它会引发意外的令牌语法错误
这是我的 server.js:
const express = require("express");
const graphqlHTTP = require("express-graphql");
const schema = require("./schema");
const app = express();
app.get(
"/graphql",
graphqlHTTP({
schema: schema,
graphiql: true
})
);
app.listen(4000, () => {
console.log("Server listening to port 4000...");
});
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLNotNull
} = require("graphql");
// HARD CODED DATA
const customers = [
{ id: "1", name: "John Doe", email: "jdoe@gmail.com", age: 35 },
{ id: "2", name: "Kelly James", email: …Run Code Online (Sandbox Code Playgroud) 我正在另一个hasura graphql模式中设置远程模式,如何使用 node/express js 设置reload_remote_schema?
我面临以下问题。在单击重新加载按钮之前,Graphql 远程架构数据不会更新。我不想手动重新加载远程模式,它应该通过节点/express js 代码自动刷新。
我收到了这种类型的查询
query {
searchRandom (param : MyObjectClass){
city
}
}
Run Code Online (Sandbox Code Playgroud)
如何设置 param 的类型MyObjectClass并将其传递到查询中?能在这里测试吗?
我是 GraphQL 的新手,打算使用 GraphQL 构建解决方案。
一切看起来都很酷,但只关心如何在 GraphQL 服务器中实现基于角色的授权(我正在考虑使用 GraphQL.js/apollo 服务器)
我将有一个包含所有用户的用户表。在用户表中有一个角色字段,其中包含特定用户的角色。将根据用户的角色授予查询和更改。
我怎样才能实现这个结构?
谢谢!
我有这个代码:
\n\nconst ProductType = new GraphQLObjectType({\n name: 'Product',\n fields: {\n id: { type: GraphQLID },\n name: { type: GraphQLString },\n category: {\n type: CategoryType,\n resolve: async (parent) => {\n return await Category.findOne({_id: parent.category});\n }\n }\n }\n});\n\nconst CategoryType = new GraphQLObjectType({\n name: 'Category',\n fields: {\n id: { type: GraphQLID },\n name: { type: GraphQLString },\n products: {\n type: ProductType,\n resolve: async (parent, args) => {\n return await Product.find({category: parent._id});\n }\n }\n }\n});\n\nconst Query = new GraphQLObjectType({\n name: 'Query',\n fields: {\n …Run Code Online (Sandbox Code Playgroud) express-graphql ×10
graphql ×10
graphql-js ×4
node.js ×4
express ×2
mongodb ×2
apollo ×1
curl ×1
graphql-java ×1
hasura ×1
javascript ×1
jestjs ×1
mongoose ×1
post ×1