Add GraphQL Custom Resolver extension type #4448
Replies: 4 comments 11 replies
-
I'm having some trouble understanding what you mean.. What do you mean by forward an endpoint to a custom GraphQL query ? |
Beta Was this translation helpful? Give feedback.
-
Hey there! I have this feature running locally (quick and dirty POC) but I think we should discuss the data structure before I submit a PR. Basically I have a simple endpoint with two additional properties: query and mutation (I did not test any mutation but it's the same principle. For the query I started with you snippet above) module.exports = {
handler: (router) => {
router.get('/', (req, res) => res.send('Hello, World!'));
router.get('/intro', (req, res) => res.send('Nice to meet you.'));
router.get('/goodbye', (req, res) => res.send('Goodbye!'));
},
// This has to be defined, not tested yet
mutation: {},
query: {
id: 'test_enpoint_query',
key: 'example',
fields: {
id: 'Int!',
firstName: 'String',
lastName: 'String',
},
args: { id: 'Int!' },
// eslint-disable-next-line no-unused-vars
async resolver(_, input) {
// Do whatever
console.log('In the resolver', input);
return {
id: 123,
firstName: 'String',
lastName: 'String',
};
},
},
}; In GraphQL service I will then fetch all this properties to build the schema: const extensionManager = getExtensionManager();
extensionManager.getEndpoints().map((endpoint) => {
const endpointPath = path.resolve(endpoint.path, endpoint.entrypoint || '');
const endpointInstance: EndpointConfig | { default: EndpointConfig } = require(endpointPath);
const mod = getModuleDefault(endpointInstance);
if (mod.query) {
// @ts-ignore
const { id, fields, resolver, args } = mod.query;
// Todo : show a warning if any of these is not available
// Create type for endpoint query
schemaComposer.Query.addFields({
[id]: {
type: schemaComposer.createObjectTC({
name: `Endpoint_${id}`,
fields,
}),
resolve: (_, input) => resolver(_, input),
...(args && { args }),
},
});
}
}); What are you thoughts? I still need to figure out how to pass GraphQL and directus contexts to the resolver, as mentioned earlier |
Beta Was this translation helpful? Give feedback.
-
Work in progress here: #9395 |
Beta Was this translation helpful? Give feedback.
-
Heya! Thanks for opening this feature request! This feature request has received over 15 votes from the community. This means we'll move this feature request to the Under Review state! The Core team will schedule a meeting to review this request as soon as possible. The discussion will then be approved or denied. You may or may not be invited to join this meeting with the core team. For more information, see our Feature Request Process. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to forward a new custom endpoint (https://docs.directus.io/guides/api-endpoints/) to custom GraphQL query?
Beta Was this translation helpful? Give feedback.
All reactions