GraphQL Execute
OcopJS - Để sử dụng truy vấn GraphQL API từ chính máy chủ.
Lưu ý sau khi phiên bản KeystoneJS 5 chuyển sang chế độ duy trì để ra mắt phiên bản mới hơn. Chúng tôi đã dựa trên mã nguồn cũ này để phát triển một phiên bản khác với một số tính năng theo hướng microservices.
Thư viện này xử dụng hàm ocop.executeGraphQL
để phát triển thêm, giúp việc
truy cấn ở máy chủ dễ hơn. Không phải viết câu truy vấn GraphQL và gọi thông
thường.
yarn add @ocopjs/server-side-graphql-client
Tạo người dùng mới thông thường sẽ được thực hiện bằng ocop.executeGraphQL
như
sau:
const { data, errors } = await ocop.executeGraphQL({
query: `mutation ($item: createUserInput){
createUser(data: $item) {
id
name
}
}`,
variables: { item: { name: "alice" } },
});
const user = data.createUser;
Xài thư viện @ocopjs/server-side-graphql-client
này chúng ta có thể viết gọn hơn
là:
const { createItem } = require("@ocopjs/server-side-graphql-client");
const user = await createItem({
ocop,
listkey: "User",
item: { name: "alice" },
returnfields: `id name`,
});
There are three key differences between ocop.executeGraphQL
and createItem
(and other functions from this package):
- If there is an error,
createItem
will be thrown as an exception, rather than providing the error as a return value. createItem
runs with access control disabled. This is suitable for use cases such as seeding data or other server side scripts where the query is triggered by the system, rather than a specific user. This can be controlled with thecontext
option.- All queries are internally paginated and all mutations are internally
chunked. This can be controlled with the
pageSize
option.
Use cases
These utilities can be used for a wide range of specific use-cases, some more common examples might include simple data seeding:
const seedUsers = async (usersData) => {
await createItems({ ocop, listKey: "User", items: usersData });
};
or fetching data inside hooks:
// This example will copy data from a related field if set
ocop.createList("Page", {
fields: {
name: { type: Text },
content: { type: Text },
copy: { type: Relationship, ref: "Page" },
},
hooks: {
resolveInput: async ({ resolvedData }) => {
// Whenever copy field is set fetch the related data
const pageToCopy = resolvedData.copy
? await getItem({
ocop,
listKey: "Page",
itemId: resolvedData.copy,
returnFields: "name, content",
})
: {};
// resolve data from the copied item and unset the relationship
return { ...resolvedData, ...pageToCopy, copy: undefined };
},
},
});
API
To perform CRUD operations, use the following functions:
For custom queries use runCustomQuery
.
NOTE: All functions accept a config object as an argument, and return a
Promise
.
Shared Config Options
The following config options are common to all server-side graphQL functions.
Properties | Type | Default | Description |
---|---|---|---|
ocop | Ocop | (required) | Ocop instance. |
listKey | String | (required) | Ocop list name. |
returnFields | String | id | A graphQL fragment of fields to return. Must match the graphQL return type. |
context | Object | N/A | An Apollo context object. See the server side graphQL docs for more details. |
NOTE: If
context
argument is provided then theocop
argument is not required.
createItem
Create a single item.