如何将对象数组传递给 Query?
How can I pass an array of objects into Query?
我正在尝试弄清楚如何将对象数组传递到我的 GraphQL 查询中,但是我发现文档对如何操作不太清楚。我在 FE 中使用 Apollo,在 BE 中使用 Graphql-yoga,并使用 Prisma 作为我的数据库以及他们的 API.
这是我的硬编码对象数组查询:
const USERS = gql`
query USERS(
$userId: ID
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: [
{ itemId: 1 },
{ itemId: 2 }
]
}
}
) {
firstName
}
}
`;
上面的查询returns我想要什么,我有点卡住的是如何得到这个数组:
[
{ itemId: 1 },
{ itemId: 2 }
]
作为查询的变量传入。根据我在网上找到的内容,我可能需要在客户端创建一个 GraphQLObjectType
才能传递对象定义。这是我的实现:
import { GraphQLObjectType, GraphQLString } from 'graphql';
const ProductName = new GraphQLObjectType({
name: 'ProductName',
fields: () => ({
itemId: {
type: GraphQLString,
},
})
});
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: [ProductName]
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
以上returns我的错误如下:
Unknown type "ProductName"
我在这里是否采用了正确的方法来传递对象数组,如果是这样,我的实现有什么问题吗?
由于 gql 函数需要一个模板文字,您应该像这样转义产品对象:
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: [${ProductName}]
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
graphql 的新手。但是想知道这是否可以解决它。
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: GraphQLList(ProductName)
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
细微变化,但我没有发表评论的特权。所以将其发布为答案。
类型已创建并用于创建您的架构服务器端。一旦创建,模式就无法在运行时修改——它具有创建时指定的任何类型和指令。换句话说,在客户端定义 new 类型是没有意义的——它不能用于您发送给服务器的任何查询,因为服务器不知道该类型.
如果将变量(如 $hasProducts
)传递给参数(如 hasProducts_some
),则该变量的类型必须与参数的类型匹配。此类型可以是标量(如 String
或 Int
),也可以是输入对象类型。那是什么类型取决于模式本身。要确定要使用的类型,您可以在 GraphQL Playground(或 GraphiQL)中打开架构的文档并搜索相关字段(在本例中为 hasProducts_some
)。
请注意,您也可以只为整个 where
字段传递一个变量。
我正在尝试弄清楚如何将对象数组传递到我的 GraphQL 查询中,但是我发现文档对如何操作不太清楚。我在 FE 中使用 Apollo,在 BE 中使用 Graphql-yoga,并使用 Prisma 作为我的数据库以及他们的 API.
这是我的硬编码对象数组查询:
const USERS = gql`
query USERS(
$userId: ID
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: [
{ itemId: 1 },
{ itemId: 2 }
]
}
}
) {
firstName
}
}
`;
上面的查询returns我想要什么,我有点卡住的是如何得到这个数组:
[
{ itemId: 1 },
{ itemId: 2 }
]
作为查询的变量传入。根据我在网上找到的内容,我可能需要在客户端创建一个 GraphQLObjectType
才能传递对象定义。这是我的实现:
import { GraphQLObjectType, GraphQLString } from 'graphql';
const ProductName = new GraphQLObjectType({
name: 'ProductName',
fields: () => ({
itemId: {
type: GraphQLString,
},
})
});
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: [ProductName]
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
以上returns我的错误如下:
Unknown type "ProductName"
我在这里是否采用了正确的方法来传递对象数组,如果是这样,我的实现有什么问题吗?
由于 gql 函数需要一个模板文字,您应该像这样转义产品对象:
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: [${ProductName}]
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
graphql 的新手。但是想知道这是否可以解决它。
const USERS = gql`
query USERS(
$userId: ID,
$hasProducts: GraphQLList(ProductName)
) {
users(
where: {
id_not: $userId
hasProducts_some: {
OR: $hasProducts
}
}
) {
firstName
}
}
`;
细微变化,但我没有发表评论的特权。所以将其发布为答案。
类型已创建并用于创建您的架构服务器端。一旦创建,模式就无法在运行时修改——它具有创建时指定的任何类型和指令。换句话说,在客户端定义 new 类型是没有意义的——它不能用于您发送给服务器的任何查询,因为服务器不知道该类型.
如果将变量(如 $hasProducts
)传递给参数(如 hasProducts_some
),则该变量的类型必须与参数的类型匹配。此类型可以是标量(如 String
或 Int
),也可以是输入对象类型。那是什么类型取决于模式本身。要确定要使用的类型,您可以在 GraphQL Playground(或 GraphiQL)中打开架构的文档并搜索相关字段(在本例中为 hasProducts_some
)。
请注意,您也可以只为整个 where
字段传递一个变量。