TwentyT
Twenty2y ago
11 replies
greg [iero]

Create entries in GraphQL

Hello all

I try to use graphql to read and add objects to Twenty.

I succeeded to get data, ie name, id and methodologies for a given "project" :

query MyQuery {
  project(filter: {verraId: {eq: 4890}}) {
    name
    id
    methodologies {
      edges {
        node {
          id
        }
      }
    }
  }
}


And I get:

{
  "data": {
    "project": {
      "name": "Anlo wetlands",
      "id": "9ae9ced6-f28c-4654-ae3d-8cdc1e035db9",
      "methodologies": {
        "edges": [
          {
            "node": {
              "id": "cb2791ff-b960-40e6-95fe-2a76708fdd02"
            }
          }
        ]
      }
    }
  }
}


If I try to create a new project :

query = gql(
        """
        mutation createProject($project: ProjectCreateInput!) {
            createProject(data: $project) {
                id
            }
        }
    """
    )

    variables = {
        "project": {
            'name': name, 
            'countryId': id,
            'verraId': vid,
            'methodologies': {
                'edges': [
                    {
                        'node': {
                            'id': mid
                        }
                    }
                ]
            }
        }
    }


    results = client.execute(query, variable_values=variables)


It does work for simple fields as name or 1 to 1 relations but not with 1 to many (here the methodologies field could countains several Methodology objects):

gql.transport.exceptions.TransportQueryError: {'message': 'Variable "$project" got invalid value { name: "My New Project", countryId: "60d7ea0a-a5e1-4fc7-8dc8-78c1f39112a2", verraId: 1000, methodologies: { edges: [[Object]] } }; Field "methodologies" is not defined by type "ProjectCreateInput".', 'locations': [{'line': 1, 'column': 24}]}


Any idea how I can do that?

Thanks !
Was this page helpful?