Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions components/slab/actions/get-posts/get-posts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import slab from "../../slab.app.mjs";

export default {
key: "slab-get-posts",
name: "Get Posts",
description: "Get posts by ID. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.posts)",
version: "0.0.1",
type: "action",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: true,
},
props: {
slab,
postIds: {
type: "string[]",
label: "Post IDs",
description: "Select one or more posts to retrieve",
options: async function({ prevContext }) {
return this.slab.listPostsForOptions({
cursor: prevContext?.cursor,
});
},
},
},
async run({ $ }) {
const query = `
query GetPosts($ids: [ID!]!) {
posts(ids: $ids) {
id
title
linkAccess
archivedAt
publishedAt
insertedAt
updatedAt
version
content
banner {
original
thumb
preset
}
owner {
id
name
email
}
topics {
id
name
}
}
}
`;
const variables = {
ids: this.postIds,
};
const response = await this.slab._makeRequest({
$,
data: {
query,
variables,
},
});
const posts = response.posts || [];
$.export("$summary", `Successfully retrieved ${posts.length} post(s)`);
return posts;
},
};

79 changes: 79 additions & 0 deletions components/slab/actions/list-posts/list-posts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import slab from "../../slab.app.mjs";
import { LIST_POSTS_QUERY } from "../../common/queries.mjs";

export default {
key: "slab-list-posts",
name: "List Posts",
description: "List posts in the organization. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.search)",
version: "0.0.1",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: true,
},
type: "action",
props: {
slab,
first: {
propDefinition: [
slab,
"first",
],
},
after: {
propDefinition: [
slab,
"after",
],
},
last: {
propDefinition: [
slab,
"last",
],
},
before: {
propDefinition: [
slab,
"before",
],
},
},
async run({ $ }) {
const query = LIST_POSTS_QUERY;
const variables = {
query: "",
...(this.first && {
first: parseInt(this.first),
}),
...(this.after && {
after: this.after,
}),
...(this.last && {
last: parseInt(this.last),
}),
...(this.before && {
before: this.before,
}),
};
const response = await this.slab._makeRequest({
$,
data: {
query,
variables,
},
});
const edges = response.search?.edges || [];
const posts = edges
.map((edge) => edge.node?.post)
.filter(Boolean);
const pageInfo = response.search?.pageInfo || {};
$.export("$summary", `Successfully retrieved ${posts.length} post(s)`);
return {
posts,
pageInfo,
edges,
};
},
};

84 changes: 84 additions & 0 deletions components/slab/actions/search-posts/search-posts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import slab from "../../slab.app.mjs";
import { SEARCH_POSTS_QUERY } from "../../common/queries.mjs";

export default {
key: "slab-search-posts",
name: "Search Posts",
description: "Search for posts based on query. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.search)",
version: "0.0.1",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: true,
},
type: "action",
props: {
slab,
query: {
type: "string",
label: "Query",
description: "Search query string",
},
first: {
propDefinition: [
slab,
"first",
],
},
after: {
propDefinition: [
slab,
"after",
],
},
last: {
propDefinition: [
slab,
"last",
],
},
before: {
propDefinition: [
slab,
"before",
],
},
},
async run({ $ }) {
const query = SEARCH_POSTS_QUERY;
const variables = {
query: this.query,
...(this.first && {
first: parseInt(this.first),
}),
...(this.after && {
after: this.after,
}),
...(this.last && {
last: parseInt(this.last),
}),
...(this.before && {
before: this.before,
}),
};
const response = await this.slab._makeRequest({
$,
data: {
query,
variables,
},
});
const edges = response.search?.edges || [];
const posts = edges
.map((edge) => edge.node?.post)
.filter(Boolean);
const pageInfo = response.search?.pageInfo || {};
$.export("$summary", `Successfully found ${posts.length} post(s) matching "${this.query}"`);
return {
posts,
pageInfo,
edges,
};
},
};

96 changes: 96 additions & 0 deletions components/slab/common/queries.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
export const SEARCH_POSTS_QUERY = `
query SearchPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) {
search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) {
edges {
node {
... on PostSearchResult {
title
highlight
content
post {
id
title
linkAccess
archivedAt
publishedAt
insertedAt
updatedAt
version
content
banner {
original
thumb
preset
}
owner {
id
name
email
}
topics {
id
name
}
}
}
}
cursor
}
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
`;

export const LIST_POSTS_QUERY = `
query ListPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) {
search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) {
edges {
node {
... on PostSearchResult {
title
highlight
content
post {
id
title
linkAccess
archivedAt
publishedAt
insertedAt
updatedAt
version
content
banner {
original
thumb
preset
}
owner {
id
name
email
}
topics {
id
name
}
}
}
}
cursor
}
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
`;
Comment on lines +1 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Eliminate query duplication using GraphQL fragments.

SEARCH_POSTS_QUERY and LIST_POSTS_QUERY are nearly identical—same variables, endpoint, and response fields. Maintaining two separate queries violates the DRY principle and creates a maintenance burden if the schema changes.

Extract the repeated post fields and pagination structure into a GraphQL fragment, then reuse it in both queries:

-export const SEARCH_POSTS_QUERY = `
-  query SearchPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) {
-    search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) {
-      edges {
-        node {
-          ... on PostSearchResult {
-            title
-            highlight
-            content
-            post {
-              id
-              title
-              linkAccess
-              archivedAt
-              publishedAt
-              insertedAt
-              updatedAt
-              version
-              content
-              banner {
-                original
-                thumb
-                preset
-              }
-              owner {
-                id
-                name
-                email
-              }
-              topics {
-                id
-                name
-              }
-            }
-          }
-        }
-        cursor
-      }
-      pageInfo {
-        hasNextPage
-        hasPreviousPage
-        startCursor
-        endCursor
-      }
-    }
-  }
-`;
-
-export const LIST_POSTS_QUERY = `
-  query ListPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) {
-    search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) {
-      edges {
-        node {
-          ... on PostSearchResult {
-            title
-            highlight
-            content
-            post {
-              id
-              title
-              linkAccess
-              archivedAt
-              publishedAt
-              insertedAt
-              updatedAt
-              version
-              content
-              banner {
-                original
-                thumb
-                preset
-              }
-              owner {
-                id
-                name
-                email
-              }
-              topics {
-                id
-                name
-              }
-            }
-          }
-        }
-        cursor
-      }
-      pageInfo {
-        hasNextPage
-        hasPreviousPage
-        startCursor
-        endCursor
-      }
-    }
-  }
-`;
+const POST_SEARCH_FIELDS = `
+  fragment PostSearchFields on PostSearchResult {
+    title
+    highlight
+    content
+    post {
+      id
+      title
+      linkAccess
+      archivedAt
+      publishedAt
+      insertedAt
+      updatedAt
+      version
+      content
+      banner {
+        original
+        thumb
+        preset
+      }
+      owner {
+        id
+        name
+        email
+      }
+      topics {
+        id
+        name
+      }
+    }
+  }
+`;
+
+const SEARCH_EDGES_QUERY = `
+  query($query: String!, $first: Int, $after: String, $last: Int, $before: String) {
+    search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) {
+      edges {
+        node {
+          ...PostSearchFields
+        }
+        cursor
+      }
+      pageInfo {
+        hasNextPage
+        hasPreviousPage
+        startCursor
+        endCursor
+      }
+    }
+  }
+`;
+
+export const SEARCH_POSTS_QUERY = `${POST_SEARCH_FIELDS}\nquery SearchPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) ${SEARCH_EDGES_QUERY.split('query')[1]}`;
+
+export const LIST_POSTS_QUERY = `${POST_SEARCH_FIELDS}\nquery ListPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) ${SEARCH_EDGES_QUERY.split('query')[1]}`;

Alternatively, if the queries are truly interchangeable, consolidate them into a single exported query and use it in both actions.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In components/slab/common/queries.mjs around lines 1-95, both SEARCH_POSTS_QUERY
and LIST_POSTS_QUERY are duplicated; extract the repeated selection set (the
PostSearchResult/post fields and pagination/edges structure) into a GraphQL
fragment and reference that fragment from both queries, or if they are identical
in usage, replace the two exports with a single exported query constant and
update callers to import that one query; ensure the fragment (or single query)
includes the variables and pageInfo/edges/cursor fields so schema changes only
need one update.


7 changes: 5 additions & 2 deletions components/slab/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/slab",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Slab Components",
"main": "slab.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.0"
}
}
}
Loading
Loading