Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional fields not working? #157

Closed
seed-of-apricot opened this issue Nov 11, 2021 · 3 comments
Closed

Optional fields not working? #157

seed-of-apricot opened this issue Nov 11, 2021 · 3 comments
Labels
note/invalid Initial assumptions turned out wrong type/bug Something is not working the way it should

Comments

@seed-of-apricot
Copy link

seed-of-apricot commented Nov 11, 2021

Screenshot

Untitled

Description

I have

export const Mutation = mutationType({
  definition(t) {
    t.nonNull.field("upsertOneGame", {
      type: "Game",
      args: {
        data: arg({ type: nonNull(GameUpsertInput) }),
      },
      async resolve(_, args, ctx: Context) {
        return ctx.prisma.game.upsert({
          where: {
            id: args.data.id,
          },
          create: args.data,
          update: {},
        });
      },
    });
  },
});

// ... other inputObjectTypes

const GameCreateWithoutTeamsInput = inputObjectType({
  name: "GameCreateWithoutTeamsInput",
  definition(t) {
    t.string("id");
    t.nonNull.string("name");
    t.nonNull.field("setting", {
      type: SettingCreateNestedOneWithoutGameInput,
    });
  },
});

const SettingCreateNestedOneWithoutGameInput = inputObjectType({
  name: "SettingCreateNestedOneWithoutGameInput",
  definition(t) {
    t.field("connect", { type: WhereUniqueInput });
    t.field("connectOrCreate", {
      type: SettingCreateOrConnectWithoutGameInput,
    });
    t.field("create", { type: nonNull(SettingCreateWithoutGameInput) });
  },
});

const SettingCreateOrConnectWithoutGameInput = inputObjectType({
  name: "SettingCreateOrConnectWithoutGameInput",
  definition(t) {
    t.nonNull.field("create", { type: nonNull(SettingCreateWithoutGameInput) });
    t.nonNull.field("where", { type: nonNull(WhereUniqueInput) });
  },
});

const SettingCreateWithoutGameInput = inputObjectType({
  name: "SettingCreateWithoutGameInput",
  definition(t) {
    t.string("id");
    t.nonNull.string("map");
    t.nonNull.string("mapSize");
    t.nonNull.field("timestamp", { type: DateTime });
    t.nonNull.int("endTurn");
  },
});

based on the Prisma schema of

// ... other models

model Game {
  id        String       @id @default(uuid())
  name      String
  setting   Setting      @relation(fields: [settingId], references: [id])
  teams     InGameTeam[]
  settingId String       @unique
}

model Setting {
  id        String   @id @default(uuid())
  map       String
  mapSize   String
  timestamp DateTime
  endTurn   Int
  game      Game?
}

and generated client of

// ... other types

export type SettingCreateWithoutGameInput = {
  id?: string
  map: string
  mapSize: string
  timestamp: Date | string
  endTurn: number
}

Then shouldn't it be fine without nonNull?
Not sure if this is a bug or by the specification though.

@seed-of-apricot seed-of-apricot added the type/bug Something is not working the way it should label Nov 11, 2021
@jasonkuhrt
Copy link
Contributor

This is not a Nexus Prisma issue.

GraphQL does not make a semantic distinction between passing nothing versus passing explicit null in optional fields while Prisma does.

There is no good solution to this.

You can do ?? undefined to remove the null.

@jasonkuhrt jasonkuhrt added the note/invalid Initial assumptions turned out wrong label Dec 6, 2021
@seed-of-apricot
Copy link
Author

Thanks for the reply, I'll go with your solution.

@ricardo-valero
Copy link

It has been asked before in the previous version of nexus-prisma
graphql-nexus/nexus-plugin-prisma#863

I think reopening this issue should be considered

Maybe this works for some cases
apollographql/apollo-client#2412 (comment)

Also worth checking out

But it'd be nice to have a utility that replaces null to undefined when needed

function replaceUnwantedNullToUndefined<GraphQLArg, PrismaArg>(graphqlArg: GraphQLArg) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
note/invalid Initial assumptions turned out wrong type/bug Something is not working the way it should
Projects
None yet
Development

No branches or pull requests

3 participants