@platformatic/sql-graphql
Advanced tools
Comparing version 0.19.1 to 0.19.2
@@ -321,3 +321,3 @@ 'use strict' | ||
const res = await entity.find({ | ||
const rows = await entity.find({ | ||
where, | ||
@@ -329,22 +329,13 @@ fields, | ||
const output = [] | ||
const matchedRaws = [] | ||
// TODO this is extremely inefficient | ||
// we need a better data structure | ||
for (const pair of keys) { | ||
for (const row of res) { | ||
let target = row | ||
for (const { key, value } of pair) { | ||
if (row[key] !== value) { | ||
target = null | ||
break | ||
} | ||
} | ||
if (target) { | ||
output.push(target) | ||
} | ||
} | ||
const matchedRaw = rows.find((raw) => | ||
pair.every(({ key, value }) => raw[key] === value) | ||
) || null | ||
matchedRaws.push(matchedRaw) | ||
} | ||
return output | ||
return matchedRaws | ||
} | ||
@@ -351,0 +342,0 @@ |
@@ -43,3 +43,7 @@ 'use strict' | ||
for (const { obj } of queries) { | ||
keys.push([{ key, value: obj[originalField].toString() }]) | ||
const value = obj[originalField] != null | ||
? obj[originalField].toString() | ||
: null | ||
keys.push([{ key, value }]) | ||
} | ||
@@ -46,0 +50,0 @@ return foreign.loadMany(keys, queries, ctx) |
{ | ||
"name": "@platformatic/sql-graphql", | ||
"version": "0.19.1", | ||
"version": "0.19.2", | ||
"description": "Map SQL dbs to GraphQL", | ||
@@ -24,4 +24,4 @@ "main": "index.js", | ||
"ws": "^8.12.1", | ||
"@platformatic/sql-events": "0.19.1", | ||
"@platformatic/sql-mapper": "0.19.1" | ||
"@platformatic/sql-events": "0.19.2", | ||
"@platformatic/sql-mapper": "0.19.2" | ||
}, | ||
@@ -28,0 +28,0 @@ "dependencies": { |
@@ -584,1 +584,206 @@ 'use strict' | ||
}) | ||
test('should handle nullable relation', async ({ pass, teardown, same, equal }) => { | ||
const app = fastify() | ||
app.register(sqlMapper, { | ||
...connInfo, | ||
async onDatabaseLoad (db, sql) { | ||
pass('onDatabaseLoad called') | ||
await clear(db, sql) | ||
if (isMysql) { | ||
await db.query(sql` | ||
CREATE TABLE authors ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) | ||
); | ||
CREATE TABLE books ( | ||
id SERIAL PRIMARY KEY, | ||
title VARCHAR(42), | ||
author_id BIGINT UNSIGNED, | ||
FOREIGN KEY (author_id) REFERENCES authors(id) | ||
); | ||
`) | ||
} else if (isSQLite) { | ||
await db.query(sql` | ||
CREATE TABLE authors ( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(255) | ||
); | ||
`) | ||
await db.query(sql` | ||
CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY, | ||
title VARCHAR(42), | ||
author_id BIGINT UNSIGNED, | ||
FOREIGN KEY (author_id) REFERENCES authors(id) | ||
); | ||
`) | ||
} else { | ||
await db.query(sql` | ||
CREATE TABLE authors ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(42) | ||
); | ||
CREATE TABLE books ( | ||
id SERIAL PRIMARY KEY, | ||
title VARCHAR(42), | ||
author_id INTEGER REFERENCES authors(id) | ||
); | ||
`) | ||
} | ||
} | ||
}) | ||
app.register(sqlGraphQL) | ||
teardown(app.close.bind(app)) | ||
await app.ready() | ||
const authors = [ | ||
{ | ||
id: 1, | ||
name: 'Mark 1' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Mark 2' | ||
} | ||
] | ||
const books = [ | ||
{ | ||
id: 1, | ||
title: 'Harry 1', | ||
authorId: 1 | ||
}, | ||
{ | ||
id: 2, | ||
title: 'Harry 2', | ||
authorId: null | ||
}, | ||
{ | ||
id: 3, | ||
title: 'Harry 3', | ||
authorId: 2 | ||
} | ||
] | ||
{ | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/graphql', | ||
body: { | ||
query: ` | ||
mutation batch($inputs : [AuthorInput]!) { | ||
insertAuthors(inputs: $inputs) { | ||
id | ||
name | ||
} | ||
} | ||
`, | ||
variables: { | ||
inputs: authors | ||
} | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'authors status code') | ||
} | ||
{ | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/graphql', | ||
body: { | ||
query: ` | ||
mutation batch($inputs : [BookInput]!) { | ||
insertBooks(inputs: $inputs) { | ||
id | ||
title | ||
} | ||
} | ||
`, | ||
variables: { | ||
inputs: books | ||
} | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'books status code') | ||
} | ||
{ | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/graphql', | ||
body: { | ||
query: ` | ||
query { | ||
books (orderBy: { field: id, direction: ASC }) { | ||
id | ||
author { | ||
id | ||
} | ||
} | ||
} | ||
` | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'query books') | ||
same(res.json(), { | ||
data: { | ||
books: [ | ||
{ | ||
id: 1, | ||
author: { | ||
id: 1 | ||
} | ||
}, | ||
{ | ||
id: 2, | ||
author: null | ||
}, | ||
{ | ||
id: 3, | ||
author: { | ||
id: 2 | ||
} | ||
} | ||
] | ||
} | ||
}, 'query book response') | ||
} | ||
{ | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/graphql', | ||
body: { | ||
query: ` | ||
query { | ||
authors { | ||
id | ||
books { | ||
id | ||
} | ||
} | ||
} | ||
` | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'query authors') | ||
same(res.json(), { | ||
data: { | ||
authors: [ | ||
{ | ||
id: 1, | ||
books: [{ id: 1 }] | ||
}, | ||
{ | ||
id: 2, | ||
books: [{ id: 3 }] | ||
} | ||
] | ||
} | ||
}, 'query authors response') | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
264377
10339