2

I have this GraphQL query:

mutation CreateBudget(
  $revenue: Float!,
  $hours: Int!,
  $projectId: Int!,
) {
  createBudget(
    revenue: $revenue,
    hours: $hours,
    projectId: $projectId,
  ) {
    id
  }
}

For best practices, I want to use camelcase here, but snake_case in my database. The table looks like this:

CREATE TABLE IF NOT EXISTS `budgets` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `revenue` double(8,2) NOT NULL,
  `hours` int(11) NOT NULL,
  `project_id` int(10) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `budgets_project_id_foreign` (`project_id`),
  CONSTRAINT `budgets_project_id_foreign` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

I use Lighthouse's @rename directive in order to convert between the casings. When running the query however, when submitting my query, some part of the code doesn't seem to recognise the project id, resulting in the following SQL error:

SQLSTATE[HY000]: General error: 1364 Field 'project_id' doesn't have a default value (SQL: insert into `budgets` (`revenue`, `hours`, `updated_at`, `created_at`) values (1200, 12, 2019-12-03 09:12:13, 2019-12-03 09:12:13))

The variables sent are

variables: {
    revenue: 1200,
    hours: 12,
    projectId: 1
}

This is how my schema.graphql looks, using the @rename directive on the Budget type:

type Budget {
  id: ID!
  revenue: Float!
  hours: Int!
  projectId: Int! @rename(attribute: "project_id")
  project: Project! @belongsTo
  created_at: DateTime
  updated_at: DateTime
}

type Mutation {
    createBudget(
        revenue: Float!
        hours: Int!
        projectId: Int!
    ): Budget @create(model: "App\\Models\\Budget\\Budget")
}

I must have overlooked something simple, but I can't seem to spot it. Anybody want to give it a shot?

Axel Köhler
  • 911
  • 1
  • 8
  • 34

1 Answers1

1

Using the rename directive on mutations is supported as of version 4.7 and needs an extra declaration of the rename directive on the property in the mutation.

Your mutation should look like this:

type Mutation {
    createBudget(
        revenue: Float!
        hours: Int!
        projectId: Int! @rename(attribute: "project_id")
    ): Budget @create(model: "App\\Models\\Budget\\Budget")
}
Axel Köhler
  • 911
  • 1
  • 8
  • 34
Enzo Notario
  • 639
  • 4
  • 9
  • You are wrong in both cases. Using the rename directive on the mutation doesn't work either. Furthermore, the [docs mention @rename in version 2.6](https://lighthouse-php.com/2.6/api-reference/directives.html#rename) already, so I don't think I'd need to be using master. I'm using v4.6 by the way. – Axel Köhler Dec 04 '19 at 09:19
  • Yes, `@rename` directive exists long time ago, but just few days ago it was changed to also work with Inputs (before it just worked in Type Fields). See PR: https://github.com/nuwave/lighthouse/pull/1062 – Enzo Notario Dec 04 '19 at 13:45
  • That wouldn't change anything, since I'm defining my @rename on a Type and not on an Input. – Axel Köhler Dec 04 '19 at 14:21
  • Apparently declaring the rename directive on a type only affects queries and not mutations. Using the rename directive again on the mutation directly will solve this, but only as of v4.7 (which was released only yesterday). I find this double declaration weird however and [submitted an issue](https://github.com/nuwave/lighthouse/issues/1073#issuecomment-562052134) – Axel Köhler Dec 05 '19 at 09:52