1

makeExecutableSchema与以下查询定义一起使用:

# Interface for simple presence in front-end.
type AccountType {
    email: Email!
    firstName: String!
    lastName: String!
}

# The Root Query
type Query {
    # Get's the account per ID or with an authToken.
    getAccount(
        email: Email
    )   : AccountType!
}

schema {
    query: Query
}

以及以下解析器:

export default {
    Query: {
        async getAccount(_, {email}, { authToken }) {
            /**
             * Authentication
             */
            //const user = security.requireAuth(authToken)

            /**
             * Resolution
             */
            const account = await accounts.find({email})
            if (account.length !== 1) {
                throw new GraphQLError('No account was found with the given email.', GraphQLError.codes.GRAPHQL_NOT_FOUND)
            }
            return account
        }
    }
}

当我查询时:

query {
  getAccount(email: "test@testing.com") {
    firstName
    lastName
  }
}

我在 GraphiQL 中得到以下结果:

{
  "data": {
    "getAccount": {
      "firstName": "John",
      "lastName": "Doe"
    }
  }
}

那么,有什么理由让我在结果中得到这个“getAccount”?

4

1 回答 1

2

因为getAccount 不是查询名称。它只是根查询类型上的常规字段Query

让结果与查询的形状完全相同是 GraphQL 的核心设计原则之一:

在此处输入图像描述 来自http://graphql.org/网站的截图

GraphQL 中的查询名称在query关键字之后:

query myQueryName {
  getAccount(email: "test@testing.com") {
    firstName
    lastName
  }
}
于 2017-07-21T13:31:14.240 回答