如何在 GraphQL 的 REST API 中获取具有 space、连字符的字段值
How to get field value having space, hyphen in REST API in GraphQL
REST API 端点 - https://api.jikan.moe/v3/manga/13
"Alterantive version"、"Side story" 和 "Spin-off" 字段有 space 和连字符。
common_schema.js
const { gql } = require('apollo-server');
const typeDefs = gql`
type RelatedType {
Adaptation: [RelatedSubType]
SideStory: [RelatedSubType]
Character: [RelatedSubType]
Summary: [RelatedSubType]
Other: [RelatedSubType]
AlternativeVersion: [RelatedSubType]
SpinOff: [RelatedSubType]
}
type RelatedSubType {
mal_id: ID
type: String
name: String
url: String
}
`;
module.exports = typeDefs;
如果我将字段值写为 Spin-off
或 Alternative version
那么它会在终端中给出错误。 "Spin-off"
也不行。我知道这些是无效的,但后来也试过了。
manga_resolver.js
module.exports = {
Query: {
manga: (_, { id }, { dataSources }) =>
dataSources.mangaAPI.getMangaDetail(id)
}
};
manga.js
const { RESTDataSource } = require('apollo-datasource-rest');
class MangaAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://api.jikan.moe/v3/manga/';
}
async getMangaDetail(mal_id) {
const response = await this.get(`/${mal_id}`);
return response;
}
}
module.exports = MangaAPI;
查询-
query getMangaDetail{
manga(id: 13){
related{
Adaptation{
name
}
AlternativeVersion{
name
}
SpinOff{
name
}
}
}
}
在那些有 space 和连字符的字段中得到 null
。
查询结果-
{
"data": {
"manga": {
"related": {
"Adaptation": [
{
"name": "One Piece"
}
],
"AlternativeVersion": null,
"SpinOff": null
}
}
}
}
根据 the spec,GraphQL 中的名称必须遵循以下格式:
/[_A-Za-z][_0-9A-Za-z]*/
换句话说,不允许使用空格和破折号。如果您的数据源返回 属性 格式不正确的名称,您可以只为有问题的字段提供解析器:
const resolvers = {
RelatedType: {
sideStory: (parent) => {
return parent['Side story']
},
...
}
}
REST API 端点 - https://api.jikan.moe/v3/manga/13
"Alterantive version"、"Side story" 和 "Spin-off" 字段有 space 和连字符。
common_schema.js
const { gql } = require('apollo-server');
const typeDefs = gql`
type RelatedType {
Adaptation: [RelatedSubType]
SideStory: [RelatedSubType]
Character: [RelatedSubType]
Summary: [RelatedSubType]
Other: [RelatedSubType]
AlternativeVersion: [RelatedSubType]
SpinOff: [RelatedSubType]
}
type RelatedSubType {
mal_id: ID
type: String
name: String
url: String
}
`;
module.exports = typeDefs;
如果我将字段值写为 Spin-off
或 Alternative version
那么它会在终端中给出错误。 "Spin-off"
也不行。我知道这些是无效的,但后来也试过了。
manga_resolver.js
module.exports = {
Query: {
manga: (_, { id }, { dataSources }) =>
dataSources.mangaAPI.getMangaDetail(id)
}
};
manga.js
const { RESTDataSource } = require('apollo-datasource-rest');
class MangaAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://api.jikan.moe/v3/manga/';
}
async getMangaDetail(mal_id) {
const response = await this.get(`/${mal_id}`);
return response;
}
}
module.exports = MangaAPI;
查询-
query getMangaDetail{
manga(id: 13){
related{
Adaptation{
name
}
AlternativeVersion{
name
}
SpinOff{
name
}
}
}
}
在那些有 space 和连字符的字段中得到 null
。
查询结果-
{
"data": {
"manga": {
"related": {
"Adaptation": [
{
"name": "One Piece"
}
],
"AlternativeVersion": null,
"SpinOff": null
}
}
}
}
根据 the spec,GraphQL 中的名称必须遵循以下格式:
/[_A-Za-z][_0-9A-Za-z]*/
换句话说,不允许使用空格和破折号。如果您的数据源返回 属性 格式不正确的名称,您可以只为有问题的字段提供解析器:
const resolvers = {
RelatedType: {
sideStory: (parent) => {
return parent['Side story']
},
...
}
}