twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
GNU Affero General Public License v3.0
15.69k stars 1.7k forks source link

Provide example on how to create a field. #5518

Open waynehamadi opened 3 months ago

waynehamadi commented 3 months ago
Screenshot 2024-05-21 at 6 43 20 PM

As you can see the object is empty right now: https://docs.twenty.com/rest-api/metadata#/operations/createOneField

greptile-apps[bot] commented 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:

  1. Front-end: Update the form component to include the new field. This can be done in 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>
  );
};
  1. Back-end: Implement the resolver for creating a field in 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;
  1. GraphQL Schema: Define the mutation in 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.

References

waynehamadi commented 3 months ago
Screenshot 2024-05-21 at 6 57 59 PM

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" }

charlesBochet commented 3 months ago

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

waynehamadi commented 3 months ago

I see thanks