Open waynehamadi opened 3 months ago
Disclaimer: This comment was automatically generated with AI. It can give some useful hints as to where to look for, but sometimes it can also give very inaccurate answers so takes this with a pinch of salt :).
To create a field, follow these steps:
components/FieldForm.js
.// components/FieldForm.js
import { useMutation } from '@apollo/client';
import { CREATE_FIELD } from '../graphql/mutations';
const FieldForm = () => {
const [createField] = useMutation(CREATE_FIELD);
const handleSubmit = (e) => {
e.preventDefault();
createField({ variables: { name: 'newFieldName', type: 'String' } });
};
return (
<form onSubmit={handleSubmit}>
<input type="text" name="fieldName" placeholder="Field Name" />
<button type="submit">Create Field</button>
</form>
);
};
resolvers/fieldResolver.js
.// resolvers/fieldResolver.js
const { Field } = require('../models');
const fieldResolver = {
Mutation: {
createField: async (_, { name, type }) => {
const newField = new Field({ name, type });
await newField.save();
return newField;
},
},
};
module.exports = fieldResolver;
graphql/schema.js
.// graphql/schema.js
const { gql } = require('apollo-server-express');
const typeDefs = gql`
type Field {
id: ID!
name: String!
type: String!
}
type Mutation {
createField(name: String!, type: String!): Field!
}
`;
module.exports = typeDefs;
These steps will allow you to create a new field and persist it in the database.
and the default empty object provided by the doc doesn't work: { "statusCode": 400, "message": "Unknown type \"CreateOneFieldInput\". Did you mean \"CreateFieldInput\", \"DeleteOneFieldInput\", \"CreateOneObjectInput\", \"CreateObjectInput\", or \"UpdateFieldInput\"?", "error": "Bad Request" }
I have investigated this issue, it seems that we don't have a consistent naming across our /metadata API regarding fields and objects.
Current namings:
The REST resolver is trying to compute these Input types but is lost as they are not consistent.
We should update the naming in the graphql /metadata API and then update the REST /metadata API.
I would vote for CreateOneFieldInput to be consistent with the naming of the graphql API objects / fields. (we do store objects as ObjectMetadata but we have accepted to keep the public API without the metadata key word as there is no possible confusion with ObjectRecords there)
FYI @ijreilly regarding Metadata API scope and @martmull regarding REST API
I see thanks
As you can see the object is empty right now: https://docs.twenty.com/rest-api/metadata#/operations/createOneField