Skip to content

Commit

Permalink
make seed data work better with KS6 (#7094)
Browse files Browse the repository at this point in the history
Co-authored-by: Noviny <noviny@thinkmill.com.au>
  • Loading branch information
Noviny and Noviny authored Dec 21, 2021
1 parent 1eed5d5 commit 618e52f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 62 deletions.
30 changes: 11 additions & 19 deletions examples/blog/seed-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,30 @@ export async function insertSeedData(context: KeystoneContext) {
console.log(`🌱 Inserting seed data`);

const createAuthor = async (authorData: AuthorProps) => {
let author = null;
try {
author = await context.query.Author.findOne({
where: { email: authorData.email },
query: 'id',
});
} catch (e) {}
let author = await context.query.Author.findOne({
where: { email: authorData.email },
query: 'id',
});

if (!author) {
author = await context.query.Author.createOne({
data: authorData,
query: 'id',
});
}
return author;
};

const createPost = async (postData: PostProps) => {
let authors;
try {
authors = await context.query.Author.findMany({
where: { name: { equals: postData.author } },
query: 'id',
});
} catch (e) {
authors = [];
}
let authors = await context.query.Author.findMany({
where: { name: { equals: postData.author } },
query: 'id',
});

postData.author = { connect: { id: authors[0].id } };
const post = await context.query.Post.createOne({
await context.query.Post.createOne({
data: postData,
query: 'id',
});
return post;
};

for (const author of authors) {
Expand Down
1 change: 1 addition & 0 deletions examples/rest-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type Person {

input PersonWhereUniqueInput {
id: ID
name: String
}

input PersonWhereInput {
Expand Down
2 changes: 1 addition & 1 deletion examples/rest-api/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ model Task {

model Person {
id String @id @default(cuid())
name String @default("")
name String @unique @default("")
tasks Task[] @relation("Task_assignedTo")
}
2 changes: 1 addition & 1 deletion examples/rest-api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const lists = {
}),
Person: list({
fields: {
name: text({ validation: { isRequired: true } }),
name: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
Expand Down
32 changes: 12 additions & 20 deletions examples/rest-api/seed-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,30 @@ export async function insertSeedData(context: KeystoneContext) {
console.log(`🌱 Inserting seed data`);

const createPerson = async (personData: PersonProps) => {
let person = null;
try {
person = await context.query.Person.findOne({
where: { name: personData.name },
query: 'id',
});
} catch (e) {}
let person = await context.query.Person.findOne({
where: { name: personData.name },
query: 'id',
});

if (!person) {
person = await context.query.Person.createOne({
await context.query.Person.createOne({
data: personData,
query: 'id',
});
}
return person;
};

const createTask = async (taskData: TaskProps) => {
let persons;
try {
persons = await context.query.Person.findMany({
where: { name: { equals: taskData.assignedTo } },
query: 'id',
});
} catch (e) {
persons = [];
}
let persons = await context.query.Person.findMany({
where: { name: { equals: taskData.assignedTo } },
query: 'id',
});

taskData.assignedTo = { connect: { id: persons[0].id } };
const task = await context.query.Task.createOne({
await context.query.Task.createOne({
data: taskData,
query: 'id',
});
return task;
};

for (const person of persons) {
Expand Down
1 change: 1 addition & 0 deletions examples/task-manager/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type Person {

input PersonWhereUniqueInput {
id: ID
name: String
}

input PersonWhereInput {
Expand Down
2 changes: 1 addition & 1 deletion examples/task-manager/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ model Task {

model Person {
id String @id @default(cuid())
name String @default("")
name String @unique @default("")
tasks Task[] @relation("Task_assignedTo")
}
2 changes: 1 addition & 1 deletion examples/task-manager/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const lists = {
}),
Person: list({
fields: {
name: text({ validation: { isRequired: true } }),
name: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
Expand Down
31 changes: 12 additions & 19 deletions examples/task-manager/seed-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,31 @@ export async function insertSeedData(context: KeystoneContext) {
console.log(`🌱 Inserting seed data`);

const createPerson = async (personData: PersonProps) => {
let person = null;
try {
person = await context.query.Person.findOne({
where: { name: personData.name },
query: 'id',
});
} catch (e) {}
let person = await context.query.Person.findOne({
where: { name: personData.name },
query: 'id',
});

if (!person) {
person = await context.query.Person.createOne({
data: personData,
query: 'id',
});
}
return person;
};

const createTask = async (taskData: TaskProps) => {
let persons;
try {
persons = await context.query.Person.findMany({
where: { name: { equals: taskData.assignedTo } },
query: 'id',
});
} catch (e) {
persons = [];
}
let persons = await context.query.Person.findMany({
where: { name: { equals: taskData.assignedTo } },
query: 'id',
});

taskData.assignedTo = { connect: { id: persons[0].id } };
const task = await context.query.Task.createOne({

await context.query.Task.createOne({
data: taskData,
query: 'id',
});
return task;
};

for (const person of persons) {
Expand Down

0 comments on commit 618e52f

Please sign in to comment.