我正在尝试使用react-admin作为管理面板,并使用ra-data-graphql-simple从graphql API中获取。问题是它找不到我的资源,并且出现此错误:
Unknown resource Category. Make sure it has been declared on your server side schema. Known resources are
这是我的代码。
App.js
import React, { Component } from 'react';
import buildGraphQLProvider from 'ra-data-graphql-simple';
import { Admin, Resource, Delete, ListGuesser } from 'react-admin';
import apolloClient from './apolloSetup';
import { CategoryList } from './categories';
class App extends Component {
constructor() {
super();
this.state = { dataProvider: null };
}
componentDidMount() {
buildGraphQLProvider({ clientOptions: { uri: 'http://127.0.0.1:3434/graphql' }})
.then(dataProvider => this.setState({ dataProvider }));
}
render() {
const { dataProvider } = this.state;
if (!dataProvider) {
return <div>Loading</div>;
}
return (
<Admin dataProvider={dataProvider}>
<Resource name="Category" list={ListGuesser} />
</Admin>
);
}
}
export default App;
和apolloSetup.js
import { HttpLink, createHttpLink } from 'apollo-link-http';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
const httpLink = createHttpLink({
uri: 'http://localhost:3434/graphql',
mode: 'no-cors',
});
export default new ApolloClient({
cache: new InMemoryCache(),
link: httpLink,
});
这是我的Graphql模式。 (类别的父ID始终为1。)
# source: http://127.0.0.1:3434/graphql
type Category {
body: String
disabled: Boolean
id: ID
image: Upload
parentId: ID
title: String
user: User
}
"""Autogenerated input type of ConfirmOtp"""
input ConfirmOtpInput {
mobile: String!
otp: String!
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
}
"""Autogenerated return type of ConfirmOtp"""
type ConfirmOtpPayload {
accessToken: String
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
errors: [String!]!
}
"""Autogenerated input type of CreateCategory"""
input CreateCategoryInput {
title: String!
body: String
parentId: ID
image: Upload
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
}
"""Autogenerated return type of CreateCategory"""
type CreateCategoryPayload {
category: Category
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
errors: [String!]
}
"""Autogenerated input type of CreatePost"""
input CreatePostInput {
title: String!
body: String
categoryId: ID!
image: Upload
video: Upload
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
}
"""Autogenerated return type of CreatePost"""
type CreatePostPayload {
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
errors: [String!]!
post: Post!
}
"""Autogenerated input type of GenerateOtp"""
input GenerateOtpInput {
mobile: String!
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
}
"""Autogenerated return type of GenerateOtp"""
type GenerateOtpPayload {
"""A unique identifier for the client performing the mutation."""
clientMutationId: String
result: String
}
type Mutation {
confirmOtp(input: ConfirmOtpInput!): ConfirmOtpPayload
createCategory(input: CreateCategoryInput!): CreateCategoryPayload
createPost(input: CreatePostInput!): CreatePostPayload
generateOtp(input: GenerateOtpInput!): GenerateOtpPayload
}
type Post {
body: String
category: Category
disabled: Boolean
id: ID
image: Upload
title: String
user: User
video: Upload
}
type Query {
"""List of all categories or categories of a directory"""
categories(parentId: ID): [Category!]
"""Returns the current user"""
currentUser: User!
"""Find a post by ID"""
post(id: ID!): Post
"""List of all posts"""
posts(categoryId: ID!): [Post!]
}
scalar Upload
type User {
mobile: String!
}
我应该创建自定义数据提供程序吗?如果是,如何为该架构创建它?
如果您仍然坚持这样做,建议您将架构简化为类别类型和相关的突变。有关ra-data-graphql-simple的预期GraphQL模式格式,请参见https://github.com/marmelab/react-admin/tree/master/packages/ra-data-graphql-simple#expected-graphql-schema。首先尝试将资源交换为这种格式。 ra-data-graphql-simple非常挑剔,如果它不喜欢您的架构,它将忽略该资源(因此,您的错误消息将不包含任何资源)。