放大 GraphQL 从 ID 以外的其他字段获取项目
Amplify GraphQL get item from other field than ID
我正在使用 React 和 AWS Amplify 实现一个网页。
我的 schema.graphql
文件中有以下定义:
type Calendar @model {
id: ID!
name: String
description: String
url: String!
intervals: [Interval] @connection(keyName: "byCalendar", fields: ["id"])
}
我想从 URL 字符串中获取日历。不幸的是,以下代码会引发错误:
import { API, graphqlOperation } from "aws-amplify";
import * as queries from "../../graphql/queries";
await API.graphql(graphqlOperation(queries.getCalendar, { url: "some-url"}));
Variable "$id" of required type "ID!" was not provided.
从错误来看,必须提供id。但是,我希望能够从 url.
中获取一个对象
我该怎么做?
我正在使用由 amplify 的 cli 自动生成的查询。
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const getCalendar = /* GraphQL */ `
query GetCalendar($id: ID!) {
getCalendar(id: $id) {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
`;
export const listCalendars = /* GraphQL */ `
query ListCalendars(
$filter: ModelCalendarFilterInput
$limit: Int
$nextToken: String
) {
listCalendars(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
url
createdAt
updatedAt
}
nextToken
}
}
`;
找到解决方案。我必须像这样向模型添加一个“byURL”键:
type Calendar @model @key(name: "byURL", fields: ["url", "id"], queryField: "calendarByURL") {
id: ID!
name: String
description: String
url: String
intervals: [Interval] @connection(keyName: "byCalendar", fields: ["id"])
}
然后使用该新键编写自定义查询(或根据更新的 schema.graphql 文件进行放大以重新生成查询):
export const getCalendarByURL = /* GraphQL */ `
query calendarByURL($url: String!) {
calendarByURL(url: $url) {
items {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
}
`;
这会让我做:
await API.graphql(graphqlOperation(customQueries.getCalendarByURL, { url: "some-url"}));
我正在使用 React 和 AWS Amplify 实现一个网页。
我的 schema.graphql
文件中有以下定义:
type Calendar @model {
id: ID!
name: String
description: String
url: String!
intervals: [Interval] @connection(keyName: "byCalendar", fields: ["id"])
}
我想从 URL 字符串中获取日历。不幸的是,以下代码会引发错误:
import { API, graphqlOperation } from "aws-amplify";
import * as queries from "../../graphql/queries";
await API.graphql(graphqlOperation(queries.getCalendar, { url: "some-url"}));
Variable "$id" of required type "ID!" was not provided.
从错误来看,必须提供id。但是,我希望能够从 url.
中获取一个对象我该怎么做?
我正在使用由 amplify 的 cli 自动生成的查询。
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const getCalendar = /* GraphQL */ `
query GetCalendar($id: ID!) {
getCalendar(id: $id) {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
`;
export const listCalendars = /* GraphQL */ `
query ListCalendars(
$filter: ModelCalendarFilterInput
$limit: Int
$nextToken: String
) {
listCalendars(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
url
createdAt
updatedAt
}
nextToken
}
}
`;
找到解决方案。我必须像这样向模型添加一个“byURL”键:
type Calendar @model @key(name: "byURL", fields: ["url", "id"], queryField: "calendarByURL") {
id: ID!
name: String
description: String
url: String
intervals: [Interval] @connection(keyName: "byCalendar", fields: ["id"])
}
然后使用该新键编写自定义查询(或根据更新的 schema.graphql 文件进行放大以重新生成查询):
export const getCalendarByURL = /* GraphQL */ `
query calendarByURL($url: String!) {
calendarByURL(url: $url) {
items {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
}
`;
这会让我做:
await API.graphql(graphqlOperation(customQueries.getCalendarByURL, { url: "some-url"}));