简体   繁体   English

无法从架构中的字段返回接口或联合类型

[英]Unable to return an interface or a union type from a field in the schema

I was reading the graphQL doc and I found an issue while working with Inline Fragments and Interfaces我正在阅读graphQL文档,在使用内联片段接口时发现了一个问题

They mention in the document that we can also return an interface or union type in the schema, but GraphiQL returns the following error.他们在文档中提到我们也可以在模式中返回接口或联合类型,但 GraphiQL 返回以下错误。

"Abstract type "User" must resolve to an Object type at runtime for field "Query.user". Either the "User" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function." “抽象类型“用户”必须在运行时为字段“Query.user”解析为 Object 类型。“用户”类型应提供“resolveType”function,或者每个可能的类型应提供“isTypeOf”function。”

index.js索引.js

import express from "express";
import { graphqlHTTP } from "express-graphql";
import { buildSchema } from "graphql";

const UserStatuses = Object.freeze({
    "registered": "REGISTERED",
    "active": "ACTIVE",
    "inactive": "INACTIVE"
});
const users = [
    {
        "id": 120,
        "first_name": "Admin",
        "last_name": "Kumar",
        "email": "admin@yopmail.com",
        "dob": "1980-01-01",
        "age": 42,
        "is_admin": true,
        "status": UserStatuses['active']
    },
    {
        "id": 121,
        "first_name": "Sachin",
        "last_name": "Kumar",
        "email": "sachin@yopmail.com",
        "dob": "1994-01-01",
        "age": 28,
        "status": UserStatuses['active'],
        "tasks": []
    },
    {
        "id": 122,
        "first_name": "Sumit",
        "last_name": "Kumar",
        "email": "sumit@yopmail.com",
        "dob": "1992-01-01",
        "age": 30,
        "status": UserStatuses['active'],
        "tasks": []
    },
    {
        "id": 123,
        "first_name": "Akash",
        "last_name": "Kumar",
        "email": "akash@yopmail.com",
        "dob": "1993-01-01",
        "age": 29,
        "status": UserStatuses['inactive'],
        "tasks": []
    },
    {
        "id": 124,
        "first_name": "Ravi",
        "last_name": "Kumar",
        "email": "ravi@yopmail.com",
        "dob": "1991-01-01",
        "age": 31,
        "status": UserStatuses['registered'],
        "tasks": []
    }
];

var schema = buildSchema(`
    type Query {
        user(id: Int): User
    }
    interface User {
        id: ID!
        first_name: String!
        last_name: String!
        email: String!
    }
    enum UserStatus {
        REGISTERED
        ACTIVE
        INACTIVE
    }

    type AppUser implements User {
        id: ID!
        first_name: String!
        last_name: String!
        email: String!
    
        age: Int!
        status: UserStatus!
    }
    type AdminUser implements User {
        id: ID!
        first_name: String!
        last_name: String!
        email: String!
    
        is_admin: Boolean
        status: UserStatus!
    }
`);

var root = {
    user: function ({id}) {
        let user = users.find(u => u.id == id);
        return user;
    },
}

var app = express();

// for testing purposes.
app.get('/', (req, res) => {
    return res.send("Hello World!");
});

app.use('/graphql', graphqlHTTP({
    schema,
    rootValue: root,
    pretty: true,
    graphiql: true
}))

app.listen(4000, () => console.log('Graphql Server is browse to http://localhost:4000/graphql'));

the query that I want to execute on GraphiQL我想在 GraphiQL 上执行的查询

query general{
    user(id: 120) {
        id
        first_name
        __typename
    }
}

You should be able to add a __resolveType field to your user object to disambiguate.您应该能够向您的用户 object 添加一个__resolveType字段来消除歧义。

var root = {
    user: function ({id}) {
        let user = users.find(u => u.id === id);
        user['__resolveType'] = user.is_admin ? 'AdminUser' : 'AppUser';
        return user;
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM